I am using the CentOS 7.
-
I wrote my first bash script like this.
#!/bin/bash echo 'this is my first code'
and I saved it as hello_world
-
I made a directory in my root home directory.
mkdir bin
-
Then I moved the script to the
~/bin
directory. -
Then I did this:
export PATH=~/bin:"$PATH" source ~/bin
-
Then I tried to run the script with the below command.
hello_world
but I did not see the this is my first code
but I got a bash: /root/bin/hello_world: Permission denied
error instead.
Best Answer
For a script to be executable without executing it with an explicit interpreter (as in
bash ~/bin/hello_world
), the script file has to have its "executable bit" set. This is done withchmod
(see its manual):This sets the executable bit for the owner of the file.
Or,
This sets the executable bit according to your current
umask
. Assuming that yourumask
is022
(a common default), this will make it executable for all users.The
source
step that you did is nonsense and should have given you an error message (you can't source a directory).If you need the setting of the new
PATH
to be "permanent", then add theexport PATH
line to your shell's startup file (~/.bashrc
if you're usingbash
as your interactive shell).Also, avoid working at an interactive root prompt. Use an unprivileged user account for testing and exploring, and use
sudo
from that account for those few times that you need to do administrative tasks.