Add to $PATH with Shell Script [duplicate] - bash

This question already has answers here:
How to permanently set $PATH on Linux/Unix [closed]
(24 answers)
Closed 4 years ago.
I want to add PATH on Linux Ubuntu 18.04 from file path.sh , my file include :
#!/bin/bash
export PATH="$PATH:/root/.local/bin"
chmod+x path.sh , but when i run it ./path.sh Path not add when i type echo $PATH .
but when i type in terminal export PATH="$PATH:/root/.local/bin" it added to my PATH .
Am I miss something on my file?

Let's consult man bash:
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the envi-
ronment of subsequently executed commands.
Note "subsequently executed commands", therefore the effect of your script ends, once the script ends.

source path.sh
add this step is fine.

Related

Trying to add alias in .bashrc file [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 3 years ago.
I need to add permanent alias for bash terminal so that I don't have to set them always after logging in.
I am adding alias command to .bashrc and trying to install the .bashrc file again using the source command.
pxxxxx#pxxxxx:~$ cat .bashrc | tail -2
alias name1='command1'
alias name2='command2'
pxxxxx#pxxxxx:~$
pxxxxxx#pxxxxxx:~$ cat .bashrc | grep export
export EDITOR='vi'
export SHELL= /usr/bin/bash
Now,​ whenever I am logging in into bash or running the following command:
$source .bashrc
I am getting the following error:
pxxxxx#xxxxxx:~$ source .bashrc
bash: export: `/usr/bin/bash': not a valid identifier
I think the issue is with your second export: you say it looks like:
export SHELL= /usr/bin/bash
Try changing it to read:
export SHELL="/usr/bin/bash"
The space after the equals would break setting it as a variable. The quotes aren't necessary, but help stop word splitting.
I can't reproduce the error you're having, but when I try to source a .bashrc with that in there it just fails to return.

Messed up PATH environment variable in Ubuntu 16.04 [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 5 years ago.
I tried installing Anaconda to get many python packages at once but had some issues with python IDLE where it said No package found so had to manually set the path in ~/.bashrc.
Once I set the path in ~/.bashrc the IMPORT ERROR in python IDLE was solved but I'm unable to use commands on terminal now.
I'm getting this error all the time.
sid#sids-ubuntu:~$ ls
Command 'ls' is available in '/bin/ls'
The command could not be located because '/bin' is not included in the PATH environment variable.
ls: command not found
sid#sids-ubuntu:~$ sudo
Command 'sudo' is available in '/usr/bin/sudo'
The command could not be located because '/usr/bin' is not included in the PATH environment variable.
sudo: command not found
sid#sids-ubuntu:~$ mkdir aa
Command 'mkdir' is available in '/bin/mkdir'
The command could not be located because '/bin' is not included in the PATH environment variable.
mkdir: command not found
I did export PATH=/usr/bin:/bin to find out my $PATH and content of /etc/environment. It seems both are different.
sid#sids-ubuntu:~$ export PATH=/usr/bin:/bin
sid#sids-ubuntu:~$ echo $PATH
/usr/bin:/bin
sid#sids-ubuntu:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
Doesn't Ubuntu look for $PATH in /etc/environment?
If yes, what could be the reason my $PATH is different from /etc/environment? and please help me fix it!
If not, where does Ubuntu look for $PATH? please help me fix it!
Be aware that your /etc/environment is only re-read at reboot.
When you want to change your path, be sure to include the existing part as well. To do that, add $PATH in the new path definition.
export PATH="$PATH:/usr/bin"
Looking at your problems, adding the $PATH in your ~/.bashrc should do the trick. If not, open a new terminal and show us the output of
echo $PATH
When adding some directory to PATH it's good idea not to overwrite previous value, just append desired directory (e.g. $HOME/bin), in your ~/.bashrc add at the end line (and remove any previous tampering with PATH)
export PATH="$PATH:$HOME/bin"
and run:
source ~/.bashrc
(or just open new session of terminal).
PATH is an environment variable, and therefore it is not looked up in any file.
There are several files which are sourced when bash is invoked (see the section named INVOCATION in the bash man page), and while sourcing these files, the environment variable PATH can be set, respectively manipulated. Note that .bashrc is not always processed; please read the bash man-page carefully to understand, which files are included under which condition.

Alias not working in Ubuntu 14.04 [duplicate]

This question already has answers here:
How to set an alias inside a bash shell script so that is it visible from the outside? [duplicate]
(4 answers)
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 6 years ago.
So I have added an alias for jumping to another directory like this
in my ./bashrc file , Looks like this
alias crmx="cd /var/www/crm/website-crm/"
Then i saved the file
but when I try to run
crmx
it says
command not found
Also I tried to do alias to see all the command but my command is not listed
Any idea ?
The file is ~/.bashrc (starting with a dot).
And you have to source it (reload) by doing source ~/.bashrc or just by closing and reopening your terminal.
You can also type ps to check if your shell is bash (for example if it's zsh you have put your alias in the ~/.zshrc file)
Let's suppose you are on bash.
Once you saved that file, you have to have bash read it with a command like this:
. ~/myaliasfile
or like this
source ~/myaliasfile
if the file resides in your home directory. Specify the path (relative to your home or absolute) otherwise.
Then you'd go to your .bashrc file and add the very same line to the end of it. By doing so the alias(es) will be read and made available to every single bash invocation and login.
Done!
More details here and here.

Why can't I execute this simple shell script? [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 9 years ago.
I have a file called go in ~/
All I want to do is to be able to run go and have it cd to this other dir.
This is what the go file looks like:
$ cat go
#!/bin/bash
cd ~/Desktop/rs3
I ran the line $ chmod +x go
And then the line ./go to attempt to run the file.
If i put echo whatever in the file it will print whatever to the console, but the cd command never works.
Thanks.
It doesn't work because the script runs in a subshell, so the environment is different.
What you can do is to alias go='cd ~/Desktop/3s3' - as it's an alias, the shell performs the substitution and runs the cd on itself, as if you've just typed it.
You should define the alias in your ~/.bashrc, ~/.bash_profile or any file that gets sourced when you login.

Export shell directory to terminal [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 7 years ago.
I'm creating a shell script that will allow a back functionality on my shell. What would be a good way to change the terminal's directory in the script.
Just running something like this in my back.sh file doesn't work:
cd /
Even when I run the file like
source ./back.sh
A sample file that is not working properly:
#!/bin/bash
cd ~/Movies/
when I run source ./back.sh or source back.sh
Here are my 2 cents about the difference between . and source builtins: There isn't any.
Since you mentioned that you use . for cd ../, make sure that the proper . and source are executed and are really the same thing:
user#host> type source
source is a shell builtin
user#host> type .
. is a shell builtin
Other than that /bin/bash --version may turn out helpful, too.

Resources