BASH: Strip new-line character from string (read line)


I bumped into the following problem: I'm writing a Linux bash script which does the following:

  • Read line from file
  • Strip the \n character from the end of the line just read
  • Execute the command that's in there

Example: commands.txt

ls -l
ls -ltra
ps as

The execution of the bash file should get the first line, and execute it, but while the \n present, the shell just outputs "command not found: ls" That part of the script looks like this

 read line

        if [ -n "$line" ]; then #if not empty line

                #myline=`echo -n $line | tr -d '\n'`
                #myline=`echo -e $line | sed ':start /^.*$/N;s/\n//g; t start'`

                myline=`echo -n $line | tr -d "\n"`
                $myline  #execute it

                cat $fname | tail -n+2 > $fname.txt
                mv $fname.txt $fname

Commented you have the things I tried before asking SO. Any solutions? I'm smashing my brains for the last couple of hours over this...

2/17/2012 10:50:47 AM

Accepted Answer

I always like perl -ne 'chomp and print' , for trimming newlines. Nice and easy to remember.

e.g. ls -l | perl -ne 'chomp and print'


I don't think that is your problem here though. Although I'm not sure I understand how you're passing the commands in the file through to the 'read' in your shell script.

With a test script of my own like this (

read line
if [ -n "$line" ]; then

and a sample input file like this (test.cmds)

ls -l
ls -ltra

If I run it like this ./ < test.cmds, I see the expected result, which is to run the first command 'ls' on the current working directory.

Perhaps your input file has additional non-printable characters in it ?

mine looks like this

od -c test.cmds 
0000000    l   s      \n   l   s       -   l  \n   l   s       -   l   t
0000020    r   a  \n                                                    

From your comments below, I suspect you may have carriage returns ( "\r" ) in your input file, which is not the same thing as a newline. Is the input file originally in DOS format ? If so, then you need to convert the 2 byte DOS line ending "\r\n" to the single byte UNIX one, "\n" to achieve the expected results.

You should be able to do this by swapping the "\n" for "\r" in any of your commented out lines.

9/22/2009 11:07:26 AM

Someone already wrote a program which executes shell commands: sh file

If you really only want to execute the first line of a file: head -n 1 file |sh

If your problem is carriage-returns: tr -d '\r' <file |sh

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow