You can use bash to do simple substitutions of variables, just like you can with sed. The bash trick turns out to take up less characters (bytes).

Lets set some variable

To replay it back:

or

Output of both:

 

Format

To do a simple substitution of the value/contents of a variable structure it like so. Put in the from part the thing you want to change (it can be a char or some chars), then it will replace them with the part you put in to (which can be a char or several chars). Note if mentioning special chars to escape them (example \ needs to be \\). Spaces are not considered special chars in this case.

echo ${VAR//from/to}

Then all of the parts that are from will change to to when the variable VAR is called.

Example 1

Lets replace all s with a S. You have to use the second notation type and add some parameters

Output:

  • Note: sed can be used to do the same substitutions

or

 

Example 2

Let replace all is with IZ

Output:

  • Note: sed can be used to do the same substitutions

or

 

Example 3 – filenames with spaces

What about converting an absolute path which has spaces to something with escaped spaces (backslash followed by space)

Output:

We convert spaces ” ” to “\ ” however in bash to print a backslash “\” we need to put an extra one “\\”

Output:

  • Note: sed can be used to do the same substitutions

or

The end.

Leave a Reply

Your email address will not be published. Required fields are marked *