Bernard
Bernard

Reputation: 4580

How to get input from command line in a simple bash file?

I think this is the worst error I have ever seen. I have written what I have seen also here:

Example of script but continuously I get error. My plan is to see how read command works. But it seems a dodgy error in my code or terminal. I wondered if anyone has seen similar problem?

#!/usr/bin/bash
echo "Plz enter"
read  text
echo "You entered $text"
echo $text
echo "$text"

Error:

$ . test.sh 
Plz enter
b
': not a valid identifier
You entered 


$ 

Upvotes: 0

Views: 153

Answers (3)

Sathish
Sathish

Reputation: 3557

I think, try to change the shell path as like below!

root@sathish:/tmp# vim test.sh

#!/usr/bin/bash
echo "Plz enter"
read  text
echo "You entered $text"
echo $text
echo "$text"

root@sathish:/tmp# chmod +x test.sh 
root@sathish:/tmp# ./test.sh 
-bash: ./test.sh: /usr/bin/bash: bad interpreter: No such file or directory

root@sathish:/tmp# which bash
/bin/bash

root@sathish:/tmp# ls /usr/bin/bash
ls: cannot access /usr/bin/bash: No such file or directory

root@sathish:/tmp# vim test.sh 

#!/bin/bash
echo "Plz enter"
read  text
echo "You entered $text"
echo $text
echo "$text"

root@sathish:/tmp# ./test.sh 
Plz enter
a
You entered a
a
a

Upvotes: 0

carlosvin
carlosvin

Reputation: 979

Yes I had the same error in windows with cygwin, I fixed it changing the end of line format from windows format to unix format.

You can also convert the end of line format using Notepad++: Edit > EOL Conversion > UNIX, then save and run the file.

Upvotes: 2

devnull
devnull

Reputation: 123508

Your input file contains CR+LF line endings.

Remove those and the script should work well. You could use dos2unix or any other utility to remove the CR.

Upvotes: 2

Related Questions