4

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.

The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.

Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?

5 Answers 5

12

The script needs to be executable, with:

chmod +x scriptname

(or similar).

Also, you want the script to be located in a directory that is in your PATH. To see your PATH use:

echo $PATH

Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.

You can add a directory to your PATH with:

PATH=$PATH:/name/of/my/directory

and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.

Sign up to request clarification or add additional context in comments.

3 Comments

I can run script, my question is: can I and how can I create a bash script that I can use as a command. For example instead of typing /media/data/script.sh can I configure linux to type just script
That's what the PATH environment variable is -- a list of directories to search for commands in. You can change your PATH in the file .bashrc in your home dir.
A shell alias as suggested by @Andy K is another option.
5

You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)

ln -s /path/to/script /usr/local/bin/scriptname

This should do the trick.

2 Comments

Users don't normally have write permission to /usr/local/bin. I'd recommend $HOME/bin, and add it to the $PATH in .profile or .bashrc as appropriate.
Thats of course assuming that the user has sufficent rights. Most times I use this approach because other users of the system are also able to use the command then (in case you want that). But for a user with not sufficent rights I fully agree with you :)
2

You can write a minimal wrapper in your home directory:

#!/bin/bash
exec /yourpath/yourfile.extension

And run your child script with this command ./NameOfYourScript

update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)

Otherwise, you can use an alias; you will have to amend your .bashrc

alias menu='bash /yourpath/menuScript.sh'

2 Comments

@TobySpeight - true, but that concern is out of scope for an answer here, since the question didn't include the script.
ok @TobySpeight mate you got me. Thanks for urbandictionary.com/define.php?term=turd+polishing ;)
1

Another way is to run it with:

/bin/bash /path/to/script

Then the file doesn't need to be executable.

Comments

0

Create a script as below

#!/bin/bash
Shell script to run

Export the script Path in to a variable:

export script="/path/to/script"

Type "Script" Command in the CLI to Get Shell script Executed

# script

If you want to make sure Script command should run in different sessions add below in .bashrc file

export Script="/path/to/script"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.