Bash : add cr to stdout
Hi,
Maybe it is a simple question for you guys but I a newby in bash script programming... Here it is : When in a script I do a Code:
z=$(ls -al 2>&1) Is there a simple way to output the result of the command and display it with carriage return for each new line? By the way, I want to retrieve stdrr so that's why I am using 2>&1 and var=$?. Thanks |
Two things:
- The answer is: surround your $(...) construct with double quotes. - The return status stored in var is that of the echo command. You probably want to have the var= line just under the ls -al line; however, I'm not sure you'll get the return status of ls -al as this command is burried in a subshell... Yves. |
Quote:
Well I have tested your solution but that's not seems to work... Ok, here is the code Code:
z="$(ls -al 2>&1)" Code:
total 12 drwxr-x--- 2 romain romain 4096 Aug 30 11:22 . drwx------ 10 romain romain 4096 Aug 30 11:22 .. -rwxr-x--- 1 romain romain 1303 Aug 30 11:22 test.sh Code:
total 12 I will check for the stderr stuff later. Thanks |
Yes I forgot that you ALSO have to put double quotes around the variable name as an argument to echo, which gives:
Code:
z="$(ls -al 2>&1)" |
SOLVED : Bash : add cr to stdout
Perfect! That's working.
By the way, I have tested the stderr code and it seems to catch successfully the error code (0 or 1) when the ls function is executed. |
All times are GMT -5. The time now is 11:04 AM. |