Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring User Defined Variable in Shell Scripting (csh shell)

Tags:

linux

shell

csh

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?

like image 774
Elpezmuerto Avatar asked Sep 28 '10 15:09

Elpezmuerto


People also ask

How do you declare a variable in csh?

All variables begin with a dollar sign ("$") when you actually use but don't when you assign them. Assigning variable require using a set command similar to BASIC languages. Quoting when assigning a variable is required to store a value with spaces.

What is the user defined variable in the script?

User-defined variables are variables that you define when you write a policy. You can use any combination of letters and numbers as variable names as long as the first variable starts with a letter: You do not need to initialize variables used to store single values, such as strings or integers.

What are user defined shell variables?

The Bourne shell recognizes alphanumeric variables to which string values can be assigned. To assign a string value to a name, type the following: Name=String. A name is a sequence of letters, digits, and underscores that begins with an underscore or a letter.

Which command is used to define a variable in shell?

The declare is a builtin command of the bash shell. It is used to declare shell variables and functions, set their attributes and display their values.


2 Answers

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
like image 101
andcoz Avatar answered Sep 28 '22 05:09

andcoz


csh expects that you set variables. Try

set howdy="Hello $USER"
echo $howdy
like image 43
Aaron Digulla Avatar answered Sep 28 '22 04:09

Aaron Digulla