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?
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.
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.
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.
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.
You have two errors in you code:
Try this:
#!/bin/csh
set howdy="Hello $USER \!"
echo $howdy
csh
expects that you set
variables. Try
set howdy="Hello $USER"
echo $howdy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With