Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove non printing chars from bash variable

Tags:

variables

bash

I have some variable $a. This variable have non printing characters (carriage return ^M).

>echo $a
some words for compgen
>a+="END"
>echo $a
ENDe words for compgen

How I can remove that char? I know that echo "$a" display it correct. But it's not a solution in my case.

like image 310
Stepan Loginov Avatar asked Feb 26 '14 14:02

Stepan Loginov


People also ask

How do I get rid of non-printable characters?

Step 1: Click on any cell (D3). Enter Formula =CLEAN(C3). Step 2: Click ENTER. It removes non-printable characters.

What does %% mean in Bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

How do you remove the last 4 characters on Bash?

To remove the last n characters of a string, we can use the parameter expansion syntax ${str::-n} in the Bash shell. -n is the number of characters we need to remove from the end of a string.

How do I remove a character from a string in Bash?

The tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string. For demonstration purposes, we will use a sample string and then pipe it to the tr command.


4 Answers

You could use tr:

tr -dc '[[:print:]]' <<< "$var"

would remove non-printable character from $var.

$ foo=$'abc\rdef'
$ echo "$foo"
def
$ tr -dc '[[:print:]]' <<< "$foo"
abcdef
$ foo=$(tr -dc '[[:print:]]' <<< "$foo")
$ echo "$foo"
abcdef
like image 187
devnull Avatar answered Oct 21 '22 02:10

devnull


To remove just the trailing carriage return from a, use

a=${a%$'\r'}
like image 40
chepner Avatar answered Oct 21 '22 03:10

chepner


I was trying to send a notification via libnotify, with content that may contain unprintable characters. The existing solutions did not quite work for me (using a whitelist of characters using tr works, but strips any multi-byte characters).

Here is what worked, while passing the 💩 test:

message=$(iconv --from-code=UTF-8 -c <<< "$message")
like image 3
We Are All Monica Avatar answered Oct 21 '22 02:10

We Are All Monica


As an equivalent to the tr approach using only shell builtins:

cleanVar=${var//[![:print:]]/}

...substituting :print: with the character class you want to keep, if appropriate.

like image 2
Charles Duffy Avatar answered Oct 21 '22 03:10

Charles Duffy