I have the following variable.
echo "|$COMMAND|"
which returns
|
REBOOT|
How can I remove that first newline?
Question&Answers:osI have the following variable.
echo "|$COMMAND|"
which returns
|
REBOOT|
How can I remove that first newline?
Question&Answers:osThe 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
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: bash have extglob
option to be enabled (shopt -s extglob
) in order to use *(...)
syntax.