Outils pour utilisateurs

Outils du site


issue130:labolinux2

Ceci est une ancienne révision du document !


When you're putting Bash scripts together, and portability with older systems and other types of shells isn't that important to you, then you might be pleased to discover that it's easy to make light work of your scripts by using a feature called Parameter Expansion. Over time, I've compiled a little list of the most common uses I've had for this feature, and I thought some people might find them helpful. I have to admit these examples are a little erratically listed, so, if you find that they're of interest, I'd suggest collecting your own notes.

In case you're new to Shell Scripting, many of the examples below are a modern shorthand, if you like. Just avoid using them on ten-year-old Bash and KornShell versions, and the chances are that they will work as expected on your friendly, neighbourhood Linux system. Nonetheless, Unix purists should probably stop reading at this point!

I seem to remember that this functionality was first introduced in Bash version 4.2, and is available in versions from that point release onward, but I might be wrong. You will probably receive an odd-looking error if your shell version doesn't support this functionality, but it's unlikely that anything will break horribly. Figure One shows how to find out which version of Bash you're running on a machine, executed as any unprivileged user – as are the other commands.

Let's not beat about the bush any longer. These examples are hopefully self-explanatory. There's plenty more reading online such as on the GNU site (https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html), and the Bash Hackers Wiki (http://wiki.bash-hackers.org/syntax/pe). The following examples all start simply with the command you would type, and underneath is what your shell will output (in most cases, version dependent) having hit the Enter key. Integrate these snippets into your scripts in any way that you see fit.

Food For Thought

This short list is really just a taster of this powerful functionality to whet your appetite.

- Find chars, two chars in length, after position three:

$ me=“1234567890”; echo ${me:3:2}

45

- Ignore the first three chars:

$ you=“1234567890”; echo ${you:3}

4567890

- Replace chars after matching them:

$ them=“1234567890”; echo ${them/456/XXX}

123XXX7890

- Remove chars from string:

$ blue=“1234567890”; echo ${blue/456}

1237890

- Display chars up to a delimiter:

$ red=“1234567890”; echo ${red%5*}

1234

- Show chars after a delimiter:

$ yellow=“1234567890”; echo ${yellow#*5}

67890

- Discover a variable's length (note the unusual use of hash here):

$ orange=123; length=${#orange}; echo $length

3

- Remove pattern (the front part of a variable):

$ green=“/etc/resolv.conf”; echo ${green#/etc/}

resolv.conf

- Remove pattern (the end of a variable):

$ pink=“chris_secret_file.tar.gz”; echo ${pink%.tar.gz}

chris_secret_file

- Find and Replace:

$ chris=“Containers are great !”; echo ${chris/great/fantastic}

Containers are fantastic !

- Discover a substring:

$ chrisbinnie=“www.devsecops.cc”; echo ${chrisbinnie:4:9}

devsecops

The End Is Nigh

Hopefully you agree that, with the addition of some of the examples mentioned on the URLs above, these commands make it possible to significantly speed up your scripting. I hope you enjoy putting them to good use either as one-liners on your command-line, or in shell scripts (as I mostly use them).

issue130/labolinux2.1519470548.txt.gz · Dernière modification : 2018/02/24 12:09 de auntiee