将 hash-sign-starting-string 参数传递给 shell 脚本

Pass hash-sign-starting-string parameter to a shell script

我尝试将包含在两个 # 符号之间的字符串传递给 ksh 脚本:

Will #> ./a.ksh #This is a string#

Will #>

没有任何提示,执行的输出将是:

Will #> ./a.ksh #This is a string#
#This is a string#

a.ksh 脚本下方。

a.ksh

echo 
echo ""
echo 
echo ""

I have tried to protect my </code> variable by any means I knew but I can't get anything starting with <code># to be displayed.

以下这些无效:

#> ./a.ksh #Hello
#> ./a.ksh #

但是这样做 :

#> ./a.ksh Hello#

任何 shell 大师可以解释为什么以及如何让它按预期工作?

我可以使用 \#This String#"#This String#" 转义这些字符串,但我想知道为什么 # 不自行打印。

# 被解释为评论的开始。解释器将忽略所有注释。

您有两个选择:

  1. 引用字符串使其成为文字:

    ./a.ksh "#This is a string#"

  2. 让您的脚本读取用户的输入而不是参数:

这是一个例子:

#!/bin/ksh
echo "Enter your text:"
read -r input
echo "You entered: $input"

然后运行先是程序,然后输入你的数据:

$ ./a.ksh
Enter your text:
#This is a string#
You entered: #This is a string#