7

In UNIX, I read that moving a shell script to /usr/local/bin will allow you to execute the script from any location by simply typing "[scriptname].sh" and pressing enter.

I have moved a script with both normal user and root permissions but I can't run it.

The script:

#! bin/bash

echo "The current date and time is:"
date

echo "The total system uptime is"
uptime

echo "The users currently logged in are:"
who

echo "The current user is:"
who -m

exit 0

This is what happens when I try to move and then run the script:

[myusername@VDDK13C-6DDE885 ~]$ sudo mv sysinfo.sh /usr/local/bin

[myusername@VDDK13C-6DDE885 ~]$ sysinfo.sh

bash: sysinfo.sh: command not found

4 Answers 4

11

If you want to run the script from everywhere you need to add it to your PATH. Usually /usr/local/bin is in the path of every user so this way it should work. So check if in your system /usr/local/bin is in your PATH doing, on your terminal:

echo $PATH 

You should see a lot of paths listed (like /bin, /sbin etc...). If its not listed you can add it. A even better solution is to keep all your scripts inside a directory, for example in your home and add it to your path.

To add a directory in your path you can modify your shell init scripts and add the new directories, for example if you're usin the BASH shell you can edi your .bashrc and add the line:

PATH=$PATH:/the_directory_you_want_to_add/:/another_directory/

This will append the new directories to your existing PATH.

0
1

You have to move it somewhere in your path. Try this:

echo $PATH

I bet /usr/local/bin is not listed.

I handle this by making a bin directory in my $HOME (i.e. mkdir ~/bin) and adding this to my ~/.bashrc file (make the file if you don't already have one):

export PATH=~/bin:$PATH
1

This may seem silly to mention, but did you make sure it is executable? Did you chmod +x script.sh? Does the shell script have the correct path to it's shell at the top (i.e #!/bin/bash)? Also, are you using UNIX or LINUX or FreeBSD? (last question is important)

-1

To run executable from any directory:

1)Make a bin directory under your home directory and mv your executable scripts into it.

[root@ip9-114-192-179 ~]# cd /home
[root@ip9-114-192-179 home]# mkdir bin
[root@ip9-114-192-179 home]#ls
bin  cloud-init-0.7.4-10.el7.noarch.rpm  cloud-user  epel-release-7-11.noarch.rpm       

2)Move your executable scripts in bin direcoty.

mv preeti.sh /home/bin

3)Now add it to your path variable.And source it.

[root@ip9-114-192-179 ~]# echo 'export PATH="$PATH:/home/bin"' >> /etc/profile
[root@ip9-114-192-179 ~]# source /etc/profile
[root@ip9-114-192-179 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/bin

4)Check if that path is added in path variable.

[root@ip9-114-192-179 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/bin

5)Verify if script is running from any random directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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