« Back to Shell scripting

Strip one or more characters from a variable or output

Want to delete one or more characters from a variable or piped output? There are multiple ways to achieve this using standard system utilities.

In this article we use single and double quotes as an example to strip from a variable named myvar. This variable could be filled with something like test'str"ng. With the quotes being special characters, we have to escape them. This way the shell interpreter knows that we mean an actual quote, instead of a string of characters. If you want to test the examples, you could also replace the variable name and put in actual text.

myvar="test'str\"ng"

Remove one or more characters

In the examples below we will use multiple tools to replace or delete characters from a variable or piped output from another tool. If you are not sure which tool to select, then have a look at the tr command first. It has a simple delete function to strip out characters. The other next good option is sed as it is powerful and another very common tool.

Using AWK

With AWK we can use the gsub function to replace multiple occurrences of the string. If we only want to replace the first match, then use sub.

echo "${myvar}" | awk "{ gsub(/[\"\']/, \"\"); print }"

Another option is reading the first argument of a string that you provide.

awk "BEGIN{gsub(/[\"\']/, \"\", ARGV[1]); print ARGV[1]}" "mytes\"ts'tring"

This example shows how to use AWK using arguments, which can be useful to replace strings without using echo

» Mastering the tool: AWK cheat sheet

AWK cheat sheet

Using perl

Another option is using Perl. Especially with its well-known format regular expression, it is easy to replace text.

echo "${myvar}" | perl -pe "s/[\"\']//g"

The syntax is very similar to the sed example below, so have a look at the explanation.

Using sed

Sed is powerful to when it comes to string manipulation. Therefore it is a good option to make changes to an existing string of text.

echo "${myvar}" | sed "s/[\"\']//g"
  • s/: search for a regular expression
  • [pattern]: pattern to sure
  • //: replace the matched pattern with nothing (=delete)
  • g: do this globally, so multiple times

Using tr

echo "${myvar}" | tr -d "\"\'"

As one might expect, the -d is short for --delete and removes characters.

Got another tool that should be listed here as well? Let it know!

Happy scripting!

Relevant commands in this article

Like to learn more about the commands that were used in this article? Have a look, for some there is also a cheat sheet available.

  • awkcheat sheet
  • sed
  • tr

Related articles

Like to learn more? Here is a list of articles within the same category or having similar tags.

Feedback

Small picture of Michael Boelen

This article has been written by our Linux security expert Michael Boelen. With focus on creating high-quality articles and relevant examples, he wants to improve the field of Linux security. No more web full of copy-pasted blog posts.

Discovered outdated information or have a question? Share your thoughts. Thanks for your contribution!

Mastodon icon