简体   繁体   English

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

[英]Declaring User Defined Variable in Shell Scripting (csh shell)

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? 我究竟做错了什么?

You have two errors in you code: 您的代码中有两个错误:

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

Try this: 试试这个:

#!/bin/csh

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

csh expects that you set variables. csh期望你set变量。 Try 尝试

set howdy="Hello $USER"
echo $howdy

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. 您似乎使用两个单引号代替双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM