Linux 在 Shell 脚本 (csh shell) 中声明用户定义的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3814097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:19:02  来源:igfitidea点击:

Declaring User Defined Variable in Shell Scripting (csh shell)

linuxshellcsh

提问by Elpezmuerto

I am trying to learn shell scripting and trying to create a user defined variable within the script, first:

我正在尝试学习 shell 脚本并尝试在脚本中创建一个用户定义的变量first

howdy="Hello $USER !"
echo $howdy

However, when I execute the script (./first) I get this:

但是,当我执行脚本 ( ./first) 时,我得到了这个:

howdy=Hello aaron!: Command not found.
howdy: Undefined variable.

What am I doing wrong?

我究竟做错了什么?

采纳答案by andcoz

You have two errors in you code:

您的代码中有两个错误:

  1. you are using sh syntax instead of csh one to set the variable
  2. you are not escaping the "!" character (history substitution)
  1. 您正在使用 sh 语法而不是 csh 语法来设置变量
  2. 你没有逃避“!” 字符(历史替换)

Try this:

尝试这个:

#!/bin/csh

set howdy="Hello $USER \!"
echo $howdy

回答by Aaron Digulla

cshexpects that you setvariables. Try

csh期望你set变量。尝试

set howdy="Hello $USER"
echo $howdy

回答by codaddict

You are doing

你在做

howdy=''Hello $USER !''

You need to enclose the string in double quotes as:

您需要将字符串括在双引号中,如下所示:

howdy="Hello $USER !"

You seem to be using two single quotes in place of a double quote.

您似乎使用两个单引号代替双引号。