Elpezmuerto
Elpezmuerto

Reputation: 5571

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:

howdy="Hello $USER !"
echo $howdy

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

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

What am I doing wrong?

Upvotes: 6

Views: 49783

Answers (3)

andcoz
andcoz

Reputation: 2232

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)

Try this:

#!/bin/csh

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

Upvotes: 13

codaddict
codaddict

Reputation: 454990

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.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328564

csh expects that you set variables. Try

set howdy="Hello $USER"
echo $howdy

Upvotes: 1

Related Questions