I have an input variable $1, and I only want the first four character of $1 and store it to a variable (i.e. rundate). I have the current code below:
rundate=$(echo $1 | cut -c1-4)
echo $rundate
but when I run my script
test.sh 20161234
it returns an error message
syntax error: `rundate=$' unexpected
Could anyone help here? Thank you!
Edit1: if I just have the following:
echo $1 | cut -c1-4
I will be able to print it as 2016. However, I want to assign it to a local variable that I can use it later in the script.
Edit2: Not too sure whether it's old bash or new bash, but if I change the code to
rundate='echo $1 | cut -c1-4'
echo $rundate
I will get
echo $1 | cut -c1-4