在终端上运行可执行文件

我是Ubuntu的新手,目前正因为任务而加入Ubuntu。 我想问几个问题:

  1. 如何创建运行shell脚本的新命令? 例如,在终端上键入passwd ,它会在/usr/bin/passwd上运行可执行文件。 如何使它像我的文件一样?

  2. 如何将我的shell脚本更改为像passwd这样的可执行文件?

您的脚本应如下所示:

 #!/bin/bash passwd 

将其保存在文件中,比如说password.sh或简单password ,然后使用终端中的下一个命令使其可执行:

 cd /path/to/password.sh #or cd /path/to/password chmod +x password.sh #or chmod +x password 

要从终端运行它,只需使用以下命令:

 ./password.sh #or ./password 

要么

 /path/to/password.sh #or /path/to/password 

仅使用以下命令运行:

 password.sh #or password 

您必须将脚本的路径添加到PATH。 请参见如何将目录添加到PATH? 在这个意义上。