如何将多个单词作为一个参数读取

时间:2019-02-19 21:26:27

标签: bash shell unix terminal arguments

我正在尝试编写一个shell脚本,该脚本将赋予它的第一个单词作为一个参数,并将其后的所有内容作为第二个参数。

用户必须调用我的shell脚本并在终端的一行中提供其参数:shellscript.sh word1 word2 word3 ...... wordn 如何编写这样的脚本

arg1 = word1

arg2 = word2-wordn?

1 个答案:

答案 0 :(得分:1)

使用shift删除第一个参数,然后使用"$*"串联其余参数。

#!/bin/bash
first=$1
shift
rest="$*"  # Assuming the first character of $IFS is a space.

printf '<%s>\n' "$first" "$rest"