BASH | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

6th Aug 2019, 1:08 AM
Henry Diaz
Henry Diaz - avatar
11 Answers
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?
6th Aug 2019, 3:07 AM
Zeke Williams
Zeke Williams - avatar
0
yes writing in vi
6th Aug 2019, 3:08 AM
Henry Diaz
Henry Diaz - avatar
0
Your bash variables can't start with a single quote '
6th Aug 2019, 3:22 AM
Zeke Williams
Zeke Williams - avatar
0
my code is echo what is your name read varname echo hello how are you $varname
6th Aug 2019, 3:25 AM
Henry Diaz
Henry Diaz - avatar
0
Try: #!/bin/bash echo "what is your name" read varname echo "hello how are you $varname" The quotes might help
6th Aug 2019, 1:43 PM
Zeke Williams
Zeke Williams - avatar
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)"
8th Aug 2019, 1:20 PM
Zeke Williams
Zeke Williams - avatar
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.
8th Aug 2019, 1:51 PM
Gwlanbzh
Gwlanbzh - avatar
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
8th Aug 2019, 2:58 PM
Zeke Williams
Zeke Williams - avatar
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
8th Aug 2019, 3:00 PM
Henry Diaz
Henry Diaz - avatar
- 1
Zeke Williams The variable must be out of the quotes
8th Aug 2019, 12:08 PM
Gwlanbzh
Gwlanbzh - avatar
- 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.
8th Aug 2019, 4:03 PM
Gwlanbzh
Gwlanbzh - avatar