+ 1
BASH
Running code in Linux with vi & the output keeps getting ânot a valid identifiersh: line 13: read: âvarnameâ Not sure what Iâm doing wrong can anyone help ?
11 Respostas
+ 1
Try:
#!/bin/bash
echo "what is your name"
read varname
echo "hello how are you $varname"
The quotes might help
0
What do you mean by running code with vi? You mean writing the code in vim and then running it in the shell? Can you post your shell script here?
0
yes writing in vi
0
Your bash variables can't start with a single quote '
0
my code is
echo what is your name
read varname
echo hello how are you $varname
0
SpaceNerd I don't know if that is true. I'll have to try it when I get to work, but I'm almost positive that bash will expand the variable inside the quotes. I even have a PS1 that has variables inside a quote, so I know it works there. Otherwise, you can just do:
echo "hello how are you $(varname)"
0
Zeke Williams In TTY in Bash, between quotes it works neither with square brackets, curly brackets, parentheses nor nothing. Only when there is no quote.
0
SpaceNerd I just tried it at work and it worked just like I thought it would. I don't know what you are talking about. Here's my code:
#!/bin/bash
thing=55
echo "this is the thing: $thing"
Here's my execution of the code:
bash test.sh
this is the thing: 55
0
yeah sorry for the late update I just made a new file and tried it & worked fine! my professor said she doesnt know why it didnt work but now it does haha whatever gets the job done appreciate the help
- 1
Zeke Williams
The variable must be out of the quotes
- 1
Zeke Williams Ok I don't know why it didn't work for me, maybe we haven't got the same Bash version... Although if it works for Henry Diaz that's the there's no more need to discuss it.