Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute bash script from any location?

Tags:

unix

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
like image 899
Joe Essey Avatar asked Feb 01 '13 15:02

Joe Essey


People also ask

How do I run a shell script in another directory?

If you make the scrip executable with chmod 755 <nameofscript> to run it you only need to type the path to the script. When you see ./script being used it telling the shell that the script is located on the same directory you are executing it. To use full path you type sh /home/user/scripts/someScript .

How do I run a script anywhere in Linux?

Make the scripts executable: chmod +x $HOME/scrips/* This needs to be done only once. Add the directory containing the scripts to the PATH variable: export PATH=$HOME/scrips/:$PATH (Verify the result with echo $PATH .) The export command needs to be run in every shell session.

How do I run a command in a specific folder?

What to Know. Type cmd into the search bar to open the command prompt. Shift + right click in a window, then click Open PowerShell Window here to access the PowerShell interface. Open the folder you wish to access, then type cmd into the folder path at the top of the window to open a command prompt within the folder.


2 Answers

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.

like image 134
Atropo Avatar answered Sep 20 '22 21:09

Atropo


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
like image 29
gpojd Avatar answered Sep 23 '22 21:09

gpojd