Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linux - How do you run bash script as a command?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...