Bash的新手。无法将参数传递给bash脚本

时间:2012-03-16 05:29:41

标签: bash

echo $1 $2 $3 ' -> echo $1 $2 $3'
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'
echo $@ ' -> echo $@'
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#' 

这是输出

This is suppose to be the output

但我得到了这个。

Jasons-MacBook-Pro:bash jasonkim$ bash arguments.sh 
 -> echo $1 $2 $3
 -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
 -> echo $@
Number of arguments passed: 0  -> echo Number of arguments passed: $#

2 个答案:

答案 0 :(得分:3)

问题不在于你不能,而是没有

bash arguments.sh Bash Scripting Tutorial

答案 1 :(得分:0)

#!/bin/bash

for var in "$@"
do
    echo "$var"
done

$ ./scr.sh a b c

输出:

a
b
c
  • 是的,很明显,错误是你根本没有将任何参数传递给脚本......也许尝试运行:bash arguments.sh arg1 arg2 arg3