Terminal Friday: Bash [Key Expansion]

First of all I would like to apologize, since last Friday I could not write the post so today I will add an extra to make up the lost day. 🙂

Brace Expansion

In Spanish key expansion seems to me to be a function inspired by C Shell, this generates combinations between the characters that are entered inside the braces, the order it uses is from left to right. It is not complicated at all, but it is an option that will be very useful in our tours of GNU / Linux.

Example:

$ echo a {1,2,3} a1 a2 a3

When used with commas (,) generates combinations between the value a and the values ​​inside the braces. If there were no value outside the keys, it would only show once each value that the key contains.

$ echo {a, b, c} abc

Its use is not complex at all, there are other more common examples such as creating many directories in a folder

$ mkdir ~ / Jobs / {one, two, three, four, five}

This creates five folders within the jobs folder, it's like entering one command at a time. Creating the 5 directories.

There are expansion by two points .. This creates a series of numbers or characters that go from the initial value to the final value, do not use lettered numbers.

$ echo {1..5} #Correct 1 2 3 4 5 $ echo {a..f} #Correct abcdf $ echo {a..5} #Incorrect {a..5} #I think I'll never get used to this blue color in letter

We can save time by creating a cycle for

#Instead of writing $ for ((i = 1; i <= 5; i ++)); do echo "My number $ i"; done My number 1 My number 2 My number 3 My number 4 My number 5 #Save code using brace expansion. $ for i in {1..5}; do echo "My number $ i; done My number 1 My number 2 My number 3 My number 4 My number 5 #Of course it is valid to use although the output is different. $ echo" My number "{1..5} My number 1 My number 2 My number 3 My number 4 My number 5

Well I think the concept is clear, haha ​​now I will only comment that it is combinable and nested. What do I mean by this?
With combinable to which we can join one or more keys

$ echo {a..c} {1..3} a1 a2 a3 b1 b2 b3 c1 c2 c3

With nesting as many would imagine you can use expansion keys within expansion keys

$ echo {a, c {1..3}, d} a c1 c2 c3 d

And finally from Bash NOTHING it is possible to increase the values.

$ echo {0..20..2} 0 2 4 6 8 10 12 14 16 18 20

This is all for today, so thanks for reading me people 🙂

Extras

How to create a local bin

When I say a bin local I mean a directory in which we have our own scripts and it is possible to execute them as a simple command ...

Achieving this is relatively easy, the first thing you need to do is create a directory where we will save the scripts.

mkdir ~ / .bin # In this example it will be hidden

Now we have our folder to save the scripts but it won't work until we add the path of the new .bin to $ PATH
For this the file is edited bash_profile, and the line is added.

export PATH = $ PATH: ~ / .bin

And voila that is enough to create a local bin, of course it will ask for root permissions if necessary for an example we write a quick script.

#! / bin / bash echo "Hi $ 1, how are you?"

Save it with the name of Hello
The script is given execution permission and it will only be enough to call it from the terminal

$ hello wada # This will show the message Hello wada, how are you?

So with this quick trick you can run your scripts faster

That's all for today people who are well 🙂
PS Sorry for mistakes if there are, my eyes are already closing hahaha 😀


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   dhunter said

    I've been in the bash issue for a long time and I hadn't understood the brace expansions these, thanks.

    On the path trick, Fedora by default does something like this but in "~ / .local / bin", look at a snippet from the bash_profile that I brought to Jessie.

    PATH = $ PATH: $ HOME / .local / bin: $ HOME / bin
    export PATH

    1.    Ricardo said

      I have some concerns:
      1. How do I get him to give me two intervals with expansion by points; ages {1..24,55..90} and that I expand ages from 1 to 24 and continue with 55 to ninety. as I did it did not work. Why?

      2. If I want the first value to be null and continue with the numbering:
      wget: http://manga.favorito /image http://manga.favorito/imagen1
      I tried the following way but wget did not get: http://manga.favorito/imagen{, 1..42} According to me, I would have to leave the first name without a number and continue with the numbering from 1 to 42 but it was not like that. Why?

    2.    Wada said

      You are right some distros have or had a .bin but it is so that everyone can understand and see that it is possible to do it in any distro: D, Thanks for stopping by.

  2.   demo said

    Very good very good for these contributions of knowledge to the linux world and its security, some Friday I hope I could read how to format a pendrive in a terminal and burn an iso DVD / CD image of any free system in a terminal.

    1.    Wada said

      Thanks for your words brother 😀 I promise that next Friday I will do that post. And I must one about customizing Vim hahaha but I didn't want them to think it would only cover Vim.

  3.   edoardo_or said

    Excellent terminal article, the best I have read in a long time, counting several blogs that publish tutorials of this style. Thanks a lot!!

    1.    Wada said

      Thank you very much 😀 I will try to keep up.

  4.   juanli said

    Excellent Tip of the Local bin!
    Regards!

    1.    Wada said

      Excellent, great that it is useful to you, thank you very much for passing brother 😀

  5.   giskard said

    Very good! No idea about this. Thanks 🙂

    1.    Wada said

      You're welcome brother thanks to you for taking the time to read it 😀

  6.   Ricardo said

    I have some concerns:
    1. How do I get him to give me two intervals with expansion by points; ages {1..24,55..90} and that I expand ages from 1 to 24 and continue with 55 to ninety. as I did it did not work. Why?

    2. If I want the first value to be null and continue with the numbering:
    wget: http://manga.favorito/imagen http://manga.favorito/imagen1

    I tried the following way but wget did not get: http://manga.favorito/imagen{, 1..42} According to me, I would have to leave the first name without a number and continue with the numbering from 1 to 42 but it was not like that. Why?
    * Sorry but I put the first post as an answer and it was wrong in some parts

    1.    Wada said

      1.- Your logic is wrong you have to nest it hahaha try with $ echo {{1..24},{55..90}}

      2.- Same as the previous one ... $ echo "URL"{,{1..42}}

      Don't worry brother, we are here to help us 🙂

  7.   jvk85321 said

    To replace the for with echo it would look like this

    echo "My number" {1..5} $ '\ n' | sed -e: a -e '$! N; s / \ n / \ n /; ta' | sed -e: a -e '$! N; s / 5 \ n / 5 /; ta'

    but i prefer printf

    printf "I, I% d \ n" {1..5}

    and use the same concept of key expansion

    atte
    jvk85321

    1.    jvk85321 said

      How do you put the terminal boxes ????

      atte
      jvk85321

  8.   jvk85321 said

    Testing if the code tag works
    hehe

    To replace the for with echo it would look like this

    echo “Mi numero “{1..5}$’\n’ | sed -e :a -e ‘$!N;s/\n /\n/;ta’ | sed -e :a -e ‘$!N;s/5\n/5/;ta’

    pero prefiero printf

    printf “Mi numero %d\n” {1..5}

    and use the same concept of key expansion

    atte
    jvk85321

    1.    jvk85321 said

      I'm left with some bugs but it worked

      I live maluco

      Sorry for bothering

      atte
      jvk85321

      1.    Wada said

        Hahaha you answered yourself but if it is between labels no spaces ...

        And on the substitution of the for it is not necessary to do so much pipe hahahaha enough with:
        echo -e "Mi numero "{1..5}"\n\b"

        To be honest printf is the best way to print text in scripts, it is more portable but traditionally uses echo.

      2.    Wada said

        I take the spaces! hahahahaha

        let's see now 😀

      3.    Wada said

        Fuck it is "less than" code "greater than" "less than" / code "greater than" hahahaha

      4.    jvk85321 said

        The problem that echo -e is not standard, so it does not work on all systems.

        Also comes out messy
        jvk@jvktos:~$ echo -e "Mi numero "{1..5}"\n\b"
        Mi numero 1
        Mi numero 2
        Mi numero 3
        Mi numero 4
        Mi numero 5

        jvk@jvktos:~$
        And with this
        jvk@jvktos:~$ echo "Mi numero "{1..5}$'\n' | sed -e :a -e '$!N;s/\n /\n/;ta' | sed -e :a -e '$!N;s/5\n/5/;ta'
        Mi numero 1
        Mi numero 2
        Mi numero 3
        Mi numero 4
        Mi numero 5
        jvk@jvktos:~$

        I think the second presents the result better, hahaha

        atte
        jvk85321

      5.    jvk85321 said

        These labels are a bummer, hehehe, let's see if they work out now
        jvk@jvktos:~$ echo -e "Mi numero "{1..5}"\n\b"
        Mi numero 1
        Mi numero 2
        Mi numero 3
        Mi numero 4
        Mi numero 5
        jvk@jvktos:~$

        And with this
        jvk@jvktos:~$ echo "Mi numero "{1..5}$'\n' | sed -e :a -e '$!N;s/\n /\n/;ta' | sed -e :a -e '$!N;s/5\n/5/;ta'
        Mi numero 1
        Mi numero 2
        Mi numero 3
        Mi numero 4
        Mi numero 5
        jvk@jvktos:~$

        I think the second presents the result better, hahaha
        atte
        jvk85321

      6.    jvk85321 said

        Anyway, leave the spaces between the lines, that's why I hate html, hahahaha

  9.   Joaquin said

    Excellent! I clap your feet haha

    I didn't know that keys could be nested, since I never tried to use them either, it really saves a lot of code and it is also more readable this way. Thank you!

    1.    Wada said

      Thanks to you for passing brother, I'm glad it was useful utilidad

      1.    Joaquin said

        You're welcome, that's what we're here for, to share and contribute ideas. I don't know how to program, I just did a couple of things in Bash and I published in two posts some scripts that I did. I think knowing this can make code easier to understand.