0

I am writing a bash script which is a follows

#!/bin/bash

#getting the environment variable from commandline
environment=$1

echo $environment

Now when I run the script with bash ./bashScript.sh Hello , I get the following errors on line

: command not found line 2

: command not found line 5

I see that both of these lines are space and bash script is thus giving me an error

To solve it I write my script as

#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment 

But it looks kind of messy

Is there any other way to achieve this. Thanks for help in advance.

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • Did you copy it from somewhere? Sounds like you might have some stray line endings or something along those lines – arco444 Jan 23 '17 at 10:14
  • @arco444 No I have written it myself. The space between lines is due to `enter` being pressed on keyboard – Shubham Khatri Jan 23 '17 at 10:15
  • I agree with @arco444 , I try your first code fragment and it works fine for me. – acornagl Jan 23 '17 at 10:16
  • 2
    When you suspect non printable characters in a file, `hexdump` or `od` allow to make sure of the file content at the byte level: `od -xc file` gives an hexadecimal dump of the file, the output of which should make the problem evident. – Serge Ballesta Jan 23 '17 at 10:20
  • @SergeBallesta I can see `\r \n \r \n` after bash when I try the above command – Shubham Khatri Jan 23 '17 at 10:34
  • @ShubhamKhatri: Just do `dos2unix ` and try! – Inian Jan 23 '17 at 10:45

1 Answers1

1

Your comment add the precision that your lines use DOS end of lines (\r\n) when you dump it with od -xc file. To avoid it, you should make sure that your editor uses Unix end of lines (\n).

To fix it on an existing text file, you can use tr:

tr -d '\r' < dos_file > unix_file
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252