0

When I modify my path variable, I made a mistake that I enter a command source ~/.profile, then the terminal when it is opened each time always echoes nothing showing as the up half of the picture like running a some program, and I have to use 'ctrl+C' to stop it and the terminal will recover and echo 'user:path$'. Even I rebooting the computer doesn't work. How do I recover my computer?

picture

This is what happens when I do bash -x ~/.profile - paste.ubuntu.com/23347084

  • 2
    Please show the contents of ~/.bashrc and ~/.profile – user595510 Oct 18 '16 at 12:50
  • OK, but the content is too much to show here, could you give me an email address? – Zhang Oct 18 '16 at 13:34
  • Observe the result of bash -x ~/.profile – waltinator Oct 18 '16 at 13:42
  • 3
    You're doing it wrong by pasting images. It's more readabvle (for us) if you: 1. Select the text you wish to send. 2. Hold Right Mouse button down, and select "Copy", to copy text to the clipboard. 3. Edit your question on AskUbuntu. 4. Hold Right Mouse button down, and select "Paste", to paste text from the clipboard. 5. Select the text you just pasted. 6. Click on the {} icon to format the pasted text as "code". Don't use pictures for text! – waltinator Oct 18 '16 at 14:19
  • 1
    @waltinator this link is the result of running the command {bash -x ~/.profile}, thank you! [link] (paste.ubuntu.com/23347084) – Zhang Oct 19 '16 at 5:04
2

I've solved the problem based on your information.

What happens is this: Bash goes to execute .bashrc, which sources .profile. .profile then sources .bashrc, which creates a recursive loop.

You can delete the source .profile line from the end of your .bashrc and that will solve the problem.

  • thank you very much, you have solved my problem perfectly. – Zhang Oct 19 '16 at 10:57
0

Bash has a nice way of matching and removing stings from variables and arrays.

Path_to_remove="~/.profile"
PATH=${PATH//:${Path_to_remove}/}

Note above assumes that you've append or prepended to your path variable, if you've over written it then sourcing your users bashrc file can also help.

source ~/.bashrc

Or use reset to reset the current terminal

reset

note above is really good if you've ascendantaly piped binary into your terminal.

Edits

Based on your comments it sounds like you've likely overwritten the path variable then.

Next time, prior to messing with it, back it up

backup option one (to file)

echo "${PATH}" | tee -a /tmp/path.bak

backup option two (temporary)

Old_path="${PATH}"

Then make changes with the knowledge that the path can be restored via the following examples

PATH=$(cat /tmp/path.bak)
## or for option two
PATH=${Old_path}

Finally understand how path separates paths for different directory paths, hint : a colon. And where most software installs executable and/or system links; below is a list within one of my fresh chroot file systems.

  • /bin
  • /sbin
  • /usr/bin
  • /usr/sbin
  • /usr/local/bin
  • /usr/local/sbin

note the above was formatted via the following and is not how path will be presented if echo'ed.

for _path in ${PATH//:/ }; do echo "${_path}"; done

With the above we can now work on rebuilding your path variable. Note do not run the following without first backing-up your current path variable.

PATH=/bin
## Add further paths using the following syntax
PATH=${PATH}:/sbin
PATH=${PATH}:/usr/bin
PATH=${PATH}:/usr/sbin
PATH=${PATH}:/usr/local/bin
PATH=${PATH}:/usr/local/sbin

Note how in above, all but the first uses ${PATH}: to ensure that you're not overwriting but instead appending to its preexisting value. The same could be achieved using += for example PATH+=:/sbin but doing it this way is more error prone because if you forget the plus sign,it'll overwrites and if you forget the colon, it'll bugger up the last path added with the new one as it's sub directories.

  • thank you, but it seems happened nothing after running the first two command, and the third command needs to wait a long time, which also may be the cause for my terminal problem. – Zhang Oct 19 '16 at 4:59
  • this answer doesn't solve the problem – user595510 Oct 19 '16 at 12:32
  • Hey I've added some additional help for ya, based upon your comments the path has been overwritten so above is included steps for restoring the paths one at a time. You may need to add more but with above you'll know how to do it safely. – S0AndS0 Oct 19 '16 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.