我有一个 shell 脚本在 /home/joey/hello.sh
现在,我想在任何地方执行它,如pwd, cut, sort, grep。
所以,hello.sh虽然我在另一个目录中,但我只是输入。
要执行脚本,您应该使其可执行。
chmod u+x /home/joey/hello.sh
Run Code Online (Sandbox Code Playgroud)
之后你可以执行
./hello.sh # if you are in the same directory
~/hello.sh # if you are in another directory
hello.sh # if you put in a directory included in the $PATH
Run Code Online (Sandbox Code Playgroud)
您可以看到路径echo $PATH中包含的所有目录,选择一个您可以在其中写入(通常)的目录并将其放在那里~/binmv
mv ~/hello.sh ~/bin # If /home/bin is in your path
Run Code Online (Sandbox Code Playgroud)
笔记
~是表示您的主目录的缩写 ( /home/joey)