Bash 基础知识系列 #3：传递参数和接受用户输入
Bash 脚本
![](/data/attachment/album/202307/15/094330dzt3s3ygshsst39t.jpg)



> 
> 在 Bash 基础系列的这一章中，学习如何向 Bash 脚本传递参数并使它们具有交互性。
> 
> 
> 


来让 Bash 脚本有参数吧 ?


你可以通过向 Bash 脚本传递变量来使其更加有用和更具交互性。


让我通过示例详细向你展示这一点。


### 将参数传递给 Shell 脚本


当你运行 Shell 脚本时，你可以按以下方式向其中添加其他变量：



```
./my_script.sh var1 var2

```

在脚本内部，你可以使用 `$1` 作为第一个参数，`$2` 作为第二个参数，依此类推。



> 
> ? `$0` 是一个特殊变量，保存正在执行的脚本的名称。
> 
> 
> 


让我们通过一个实际的例子来看看。切换到保存练习 Bash 脚本的目录。



```
mkdir -p bash_scripts && cd bash_scripts

```

现在，创建一个名为 `arguments.sh` （我想不出更好的名称）的新 Shell 脚本，并向其中添加以下行：



```
#!/bin/bash

echo "Script name is: $0"
echo "First argument is: $1"
echo "Second argument is: $2"

```

保存文件并使其可执行。现在像往常一样运行脚本，但这次向其中添加任意两个字符串。你将看到屏幕上打印的详细信息。



> 
> ? 参数由空格（空格、制表符）分隔。如果参数中有空格，请使用（英文）双引号将其引起来，否则它将被视为单独的参数。
> 
> 
> 


![Pass arguments to the bash scripting](/data/attachment/album/202307/15/094358q0i6ji6i0wlzjgr4.png)



> 
> ? Bash 脚本最多支持 255 个参数。但对于参数 10 及以上，你必须使用花括号 `${10}`、`${11}`...`${n}`。
> 
> 
> 


正如你所看到的，`$0` 代表脚本名称，而其余参数存储在编号变量中。你还可以在脚本中使用一些其他特殊变量。




| 特殊变量 | 变量描述 |
| --- | --- |
| `$0` | 脚本名称 |
| `$1`、`$2`、……`$9` | 脚本参数 |
| `${n}` | 脚本参数从 10 到 255 |
| `$#` | 参数数量 |
| `$@` | 所有参数 |
| `$$` | 当前 Shell 的进程 ID |
| `$!` | 最后执行的命令的进程 ID |
| `$?` | 最后执行命令的退出状态 |



> 
> ?️‍♀️ 修改上面的脚本以显示参数数量。
> 
> 
> 


#### 如果参数数量不匹配怎么办？


在上面的示例中，你为 Bash 脚本提供了两个参数并在脚本中使用了它们。


但是，如果你只提供一个参数或三个参数怎么办？


让我们实际做一下吧。


![Passing fewer or more arguments to bash script](/data/attachment/album/202307/15/094358v3tirygha3xgeagb.png)


正如你在上面所看到的，当你提供的参数超出预期时，结果仍然是一样的。不使用其他参数，因此不会产生问题。


但是，当你提供的参数少于预期时，脚本将显示空白。如果脚本的一部分依赖于缺少的参数，这可能会出现问题。


### 接受用户输入并制作交互式 Bash 脚本


你还可以创建提示用户通过键盘提供输入的 Bash 脚本。这使你的脚本具有交互性。


`read` 命令提供了此功能。你可以这样使用它：



```
echo "Enter something"
read var

```

上面的 `echo` 命令不是必需的，但最终用户不会知道他们必须提供输入。然后用户在按回车键之前输入的所有内容都存储在 `var` 变量中。


你还可以显示提示消息并在单行中获取值，如下所示：



```
read -p "Enter something? " var

```

让我们看看它的实际效果。创建一个新的 `interactive.sh` Shell 脚本，内容如下：



```
#!/bin/bash

echo "What is your name, stranger?"
read name
read -p "What's your full name, $name? " full_name
echo "Welcome, $full_name"

```

在上面的示例中，我使用 `name` 变量来获取名称。然后我在提示中使用 `name` 变量，并在 `full_name` 变量中获取用户输入。我使用了两种使用 `read` 命令的方法。


现在，如果你授予执行权限，然后运行此脚本，你会注意到该脚本显示 `What is your name, stranger?`，然后等待你从键盘输入内容。你提供输入，然后它会显示 `What's your full name` 消息，并再次等待输入。


以下是供你参考的示例输出：


![Interactive bash shell script](/data/attachment/album/202307/15/094358tpp663rn649w6wrr.png)


### ?️ 练习时间


是时候练习你所学到的东西了。尝试为以下场景编写简单的 Bash 脚本。


**练习 1**：编写一个带有三个参数的脚本。你必须使脚本以相反的顺序显示参数。


预期输出：



```
abhishek@itsfoss:~/bash_scripts$ ./reverse.sh ubuntu fedora arch
Arguments in reverse order:
arch fedora ubuntu

```

**练习 2**：编写一个脚本，显示传递给它的参数数量。


提示：使用特殊变量 `$#`。


预期输出：



```
abhishek@itsfoss:~/bash_scripts$ ./arguments.sh one and two and three
Total number of arguments: 5

```

**练习 3**：编写一个脚本，将文件名作为参数并显示其行号。


提示：使用 `wc` 命令来计算行号。


你可以在社区中讨论你的解决方案。


很好！ 现在你可以（传递）参数了 ? 在下一章中，你将学习在 Bash 中执行基本数学运算。


*（题图：MJ/5a75aa2f-1cb1-4009-a4e6-683cf61bc892）*




---


via: <https://itsfoss.com/bash-pass-arguments/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Chapter #3: Passing Arguments and Accepting User Inputs

Learn how to pass arguments to bash scripts and make them interactive in this chapter of the Bash Basics series.

Let's have arguments... with your bash scripts 😉

You can make your bash script more useful and interactive by passing variables to it.

Let me show you this in detail with examples.

## Pass arguments to a shell script

When you run a shell script, you can add additional variables to it in the following fashion:

`./my_script.sh var1 var2`


Inside the script, you can use $1 for the 1st argument, $2 for the 2nd argument and so on.

Let's see it with an actual example. Switch to the directory where you keep your practice bash scripts.

```
mkdir -p bash_scripts && cd bash_scripts
```


Now, create a new shell script named `arguments.sh`

(I could not think of any better names) and add the following lines to it:

```
#!/bin/bash
echo "Script name is: $0"
echo "First argument is: $1"
echo "Second argument is: $2"
```


Save the file and make it executable. Now run the script like you always do but this time add any two strings to it. You'll see the details printed on the screen.

![Pass arguments to the bash scripting](https://itsfoss.com/content/images/2023/06/run-bash-script-with-arguments.png)

As you can see, the $0 represents the script name while the rest of the arguments are stored in the numbered variables. There are some other special variables that you may use in your scripts.

Special Variable | Description |
---|---|

$0 | Script name |

$1, $2...$9 | Script arguments |

${n} | Script arguments from 10 to 255 |

$# | Number of arguments |

$@ | All arguments together |

$$ | Process id of the current shell |

$! | Process id of the last executed command |

$? | Exit status of last executed command |


### What if the number of arguments doesn't match?

In the above example, you provided the bash script with two arguments and used them in the script.

But what if you provided only one argument or three arguments?

Let's do it actually.

![Passing fewer or more arguments to bash script](https://itsfoss.com/content/images/2023/06/passing-non-matching-arguments-bash-shell.png)

As you can see above, when you provided more than expected arguments, things were still the same. Additional arguments are not used so they don't create issues.

However, when you provided fewer than expected arguments, the script displayed empty space. This could be problematic if part of your script is dependent on the missing argument.

## Accepting user input and making an interactive bash script

You can also create bash scripts that prompt the user to provide input through the keyboard. This makes your scripts interactive.

The read command provides this feature. You can use it like this:

```
echo "Enter something"
read var
```


The echo command above is not required but then the end user won't know that they have to provide input. And then everything that the user enters before pressing the return (enter) key is stored in `var`

variable.

You can also display a prompt message and get the value in a single line like this:

`read -p "Enter something? " var`


Let's see it in action. Create a new `interactive.sh`

shell script with the following content:

```
#!/bin/bash
echo "What is your name, stranger?"
read name
read -p "What's your full name, $name? " full_name
echo "Welcome, $full_name"
```


In the above example, I used the `name`

variable to get the name. And then I use the `name`

variable in the prompt and get user input in `full_name`

variable. I used both ways of using the read command.

Now if you give the execute permission and then run this script, you'll notice that the script displays `What is your name, stranger?`

and then waits for you to enter something from the keyboard. You provide input and then it displays `What's your full name`

type of message and waits for the input again.

Here's a sample output for your reference:

![Interactive bash shell script](https://itsfoss.com/content/images/2023/06/interactive-bash-shell-script.png)

## 🏋️ Exercise time

Time to practice what you learned. Try writing simple bash scripts for the following scenarios.

**Exercise 1**: Write a script that takes three arguments. You have to make the script display the arguments in reverse order.

**Expected output**:

```
abhishek@itsfoss:~/bash_scripts$ ./reverse.sh ubuntu fedora arch
Arguments in reverse order:
arch fedora ubuntu
```


**Exercise 2**: Write a script that displays the number of arguments passed to it.

**Hint**: Use special variable $#

**Expected output**:

```
abhishek@itsfoss:~/bash_scripts$ ./arguments.sh one and two and three
Total number of arguments: 5
```


**Exercise 3**: Write a script that takes a filename as arguments and displays its line number.

**Hint**: Use wc command for counting the line numbers.

You may discuss your solution in the community.

[Practice Exercise in Bash Basics Series #3: Pass Arguments and Accept User InputsIf you are following the Bash Basics series on It’s FOSS, you can submit and discuss the answers to the exercise at the end of the chapter: Fellow experienced members are encouraged to provide their feedback to new members. Do note that there could be more than one answer to a given problem.](https://itsfoss.community/t/practice-exercise-in-bash-basics-series-3-pass-arguments-and-accept-user-inputs/10799)![](https://itsfoss.community/uploads/default/original/1X/f274f9749e3fd8b4d6fbae1cf90c5c186d2f699c.png)


![](https://itsfoss.community/uploads/default/original/1X/f274f9749e3fd8b4d6fbae1cf90c5c186d2f699c.png)

Great! So now you can (pass) argument :) In the next chapter, you'll learn to perform basic mathematics in bash.