You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the$2variable, the third argument is referenced by$3, .. etc. ...
#!/bin/bash echo "Script name is: $0" echo "First argument is: $1" echo "Second argument is: $2" 保存文件并使其可执行。现在像往常一样运行脚本,但这次向其中添加任意两个字符串。你将看到屏幕上打印的详细信息。 参数由空格(空格、制表符)分隔。如果参数中有空格,请使用(英文)双引号将其引起来...
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. 💡 $0 is a special variable that...
#!/bin/bashecho"Script name is:$0"echo"First argument is:$1"echo"Second argument is:$2" 1. 2. 3. 4. 保存文件并使其可执行。现在像往常一样运行脚本,但这次向其中添加任意两个字符串。你将看到屏幕上打印的详细信息。 🚧 参数由空格(空格、制表符)分隔。如果参数中有空格,请使用(英文)双引号将...
Navigate to a directory where your hello_world.sh is located and make the file executable: $ chmod +x hello_world.sh Now you are ready to execute your first bash script: ./hello_world.sh 2. Simple Backup bash shell script #!/bin/bash tar -czf myhome_directory.tar.gz /home/linuxconf...
让我们运行不带参数的代码: $ ./script.sh 脚本的输出: Please, pass an argument 现在,让我们用一些参数来运行它: $ ./script.sh jiyik stack blog 代码的输出: The number of input arguments are 3 The first one is jiyik
Your script can accept arguments just like a command line program! The first argument to your script is stored in$1, the second argument is stored in$2, etc, etc. An array of all of the arguments passed to your script is stored in$@, and we’ll discuss how to handle arrays later ...
The first section of the script prints out the total number of arguments to pass to the scripts and then prints out the value of each argument. That section looks like this: echo "Total Number of Arguments:" $# echo "Argument values:" $@ ...
3. Passing arguments to bash script You can pass arguments to a bash script while running it in the following manner: ./my_script.sh arg1 arg2 Inside the script, you can use $1 for the 1st argument, $2 for the 2nd argument and so on. $0 is a special variable that holds the name...
1. Using your favorite text editor, create a shell script calledsyntax. If you're using Vim, run the following line in the terminal: vim syntax.sh 2. Add the code below to the shell script: # syntax.sh# Declaring functions using the reserved word function# Multilinefunctionf1 {echoHello...