Bash - Inserting non printable character(s) in string variable

Hello.
I have a string variable named L_TEMP to test a very simple filter.
L_TEMP="50AwL.|KWp9jk"
I want to insert a non printable character [ 0x00 - 0x1F ] between K and W.
I have try this :

linux-g65k:~ # a='50AwL.|K'
linux-g65k:~ # b='Wp9jk'
linux-g65k:~ # L_TEMP="$a$'\x07'$b"
linux-g65k:~ # echo "PATTERN : $L_TEMP"
PATTERN : 50AwL.|K$'\x07'Wp9jk
linux-g65k:~ # L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")
linux-g65k:~ # echo $L_TEMP_TEST
50AwL.|K$'\x07'Wp9jk
linux-g65k:~ # 
linux-g65k:~ # 

The result should be :

linux-g65k:~ # a='50AwL.|K'
linux-g65k:~ # b='Wp9jk'
linux-g65k:~ # L_TEMP="$a$'\x07'$b"
linux-g65k:~ # echo "PATTERN : $L_TEMP"
PATTERN : 50AwL.|KWp9jk
linux-g65k:~ # L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")
linux-g65k:~ # echo $L_TEMP_TEST

linux-g65k:~ # 
linux-g65k:~ # 

Any help is welcome

Try

L_TEMP=$a$'\x07'$b

or

L_TEMP=$a$'\a'$b
1 Like

My filter is :

L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")

My filter is malformed.
I want to exclude any value which does not contains exclusively printable character.

L_TEMP=$a$'\x07'$b
L_TEMP_TEST=$(echo "$L_TEMP"  | grep -E "[[:print:]]")

L_TEMP_TEST should be null
What is the good filter ?
Any help is welcome

Not sure I understand. You want to eliminate non-printable chars from a string?

in pseudo code :

 MY_VAR=$L_TEMP
while read -N 1 ; do
   A_CHAR="$REPLY"
   if [[ $A_CHAR is not A_PRINTABLE_CHARACTER ]] ; then
    MY_VAR=""
    break
   fi
done <<<"$L_TEMP"

if [[ ! z $MY_VAR ]] ; then
    OK KEEP MY_VAR
else
    CALCULATE ANOTHER MY_VAR
fi

MY_VAR should set to null if it contains one or more not printable character
Somethings like

grep -E "[all characters are printable]"

my current filter is

grep -E "[one or more characters are printable]"

---------- Post updated at 13:09 ---------- Previous update was at 12:54 ----------

I have try

grep -e '[^[:print:]]'

Which seems not working

You could use

echo $L_TEMP | grep -v "[^[:print:]]"

or, with a recent shell,

L_TEMP_TEST=${L_TEMP//[[:print:]]}

The second would leave L_TEMP_TEST empty if no non-printable chars are in the variable, or the non-printable chars only if there were.

1 Like

That works
Great
Thank you very much