如何从任何目录执行我的 bash shell 脚本?

Joe*_*oey 5 bash shell-script

我有一个 shell 脚本在 /home/joey/hello.sh

现在,我想在任何地方执行它,如pwd, cut, sort, grep

所以,hello.sh虽然我在另一个目录中,但我只是输入。

Has*_*tur 6

要执行脚本,您应该使其可执行。

 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)

笔记