Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following variable.

echo "|$COMMAND|"

which returns

|
REBOOT|

How can I remove that first newline?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
867 views
Welcome To Ask or Share your Answers For Others

1 Answer

Under , there are some bashisms:

The tr command could be replaced by // bashism:

COMMAND=$'
REBOOT
   
'
echo "|${COMMAND}|"
|
   OOT
|

echo "|${COMMAND//[$'
']}|"
|REBOOT   |

echo "|${COMMAND//[$'
 ']}|"
|REBOOT|

See Parameter Expansion and QUOTING in bash's man page:

man -Pless +//pattern bash
man -Pless +/\'string\' bash

man -Pless +/^\ *Parameter\ Exp bash
man -Pless +/^\ *QUOTING bash

Further...

As asked by @AlexJordan, this will suppress all specified characters. So what if $COMMAND do contain spaces...

COMMAND=$'         
        RE BOOT      
           
'
echo "|$COMMAND|"
|
           BOOT      
|

CLEANED=${COMMAND//[$'
']}
echo "|$CLEANED|"
|                 RE BOOT                 |

shopt -q extglob || { echo "Set shell option 'extglob' on.";shopt -s extglob;}

CLEANED=${CLEANED%%*( )}
echo "|$CLEANED|"
|                 RE BOOT|

CLEANED=${CLEANED##*( )}
echo "|$CLEANED|"
|RE BOOT|

Shortly:

COMMAND=$'         
        RE BOOT      
           
'
CLEANED=${COMMAND//[$'
']} && CLEANED=${CLEANED%%*( )}
echo "|${CLEANED##*( )}|"
|RE BOOT|

Note: have extglob option to be enabled (shopt -s extglob) in order to use *(...) syntax.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...