• bash variable expansion

    From MeaTLoTioN@1337:1/101 to All on Tue Jan 31 10:16:35 2023
    Hi all,

    I was doing some bash scripting a little while ago and while manipulating some text using some neat shortcuts I thought I'd share
    in case anyone has a need to do just this;


    Consider a variable; `string="this is a string"`

    To output this string as is, you'd do this;
    ```
    $ echo $string
    this is a string
    ```

    If you wanted that string to output all uppercase, you could do this;
    ```
    $ echo ${string^^}
    THIS IS A STRING
    ```

    If you wanted that string to output all lowercase, you could do this;
    ```
    $ echo ${string,,}
    this is a string
    ```

    If you wanted to capitalise just the first word, you could do this;
    ```
    $ echo ${string^}
    This is a string
    ```

    If you wanted to capitalise every word, you could do this;
    ```
    $ stringArray=( $string )
    $ echo ${stringArray[@]^}
    This Is A String
    ```

    If you wanted to change a the word `is` to another word, say `isn't` then you could do this;
    ```
    $ echo "${string// is/ isn\'t}"
    this isn't a string
    ```

    If you wanted to change all the spaces into underscores, you could do this;
    ```
    $ echo "${string// /_}"
    this_is_a_string
    ```


    I hope that this will help someone =)

    ---
    |14Best regards,
    |11Ch|03rist|11ia|15n |11a|03ka |11Me|03aTLoT|11io|15N // @meatlotion:erb.pw

    |07ÄÄ |08[|10eml|08] |15ml@erb.pw |07ÄÄ |08[|10web|08] |15www.erb.pw |07ÄÄÄ¿ |07ÄÄ |08[|09fsx|08] |1521:1/158 |07ÄÄ |08[|11tqw|08] |151337:1/101 |07ÂÄÄÙ |07ÄÄ |08[|12rtn|08] |1580:774/81 |07ÄÂ |08[|14fdn|08] |152:250/5 |07ÄÄÄÙ
    |07ÄÄ |08[|10ark|08] |1510:104/2 |07ÄÙ

    --- Mystic BBS v1.12 A48 2022/07/15 (Linux/64)
    * Origin: thE qUAntUm wOrmhOlE, rAmsgAtE, uK. bbs.erb.pw (1337:1/101)
  • From dozo@1337:1/117 to MeaTLoTioN on Tue Jan 31 21:00:00 2023
    On 31 Jan 2023, MeaTLoTioN said the following...

    I was doing some bash scripting a little while ago and while
    manipulating some text using some neat shortcuts I thought I'd share
    in case anyone has a need to do just this;
    --8<-------
    I hope that this will help someone =)

    I didn't know, some of these can come in quite handy, thanks!

    dozo


    dozo

    --- Mystic BBS v1.12 A48 (Linux/64)
    * Origin: (1337:1/117)
  • From paulie420@1337:3/129 to MeaTLoTioN on Tue Jan 31 17:06:40 2023
    I was doing some bash scripting a little while ago and while
    manipulating some text using some neat shortcuts I thought I'd share
    in case anyone has a need to do just this;

    As always, I've extracted this msg and it makes its way to my text folder. Thank you for the tricks - I think I can use some of these at times. Them pesky







































































































































































    spaces and all...

    If you wanted to capitalise every word, you could do this;
    ```
    $ stringArray=( $string )
    $ echo ${stringArray[@]^}
    This Is A String
    ```

    This one is interesting... why do we have to create a new variable; or... what can't you just;
    echo ${string[@]^}

    At any rate, thanks; I'll be playing around with how to use this wizardry on my







































































































































































    own.



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A48 (Linux/64)
    * Origin: 2o fOr beeRS bbs>>>20ForBeers.com:1337 (1337:3/129)
  • From MeaTLoTioN@1337:1/101 to paulie420 on Wed Feb 1 16:23:38 2023
    On 31 Jan 2023, paulie420 said the following...

    If you wanted to capitalise every word, you could do this;
    ```
    $ stringArray=( $string )
    $ echo ${stringArray[@]^}
    This Is A String
    ```

    This one is interesting... why do we have to create a new variable;
    or... what can't you just;
    echo ${string[@]^}

    Ah good question, the reason is because you need an array to be able to iterate







































































































































































    over words in it, otherwise you'll just iterate over each character. That's why







































































































































































    I made the array first out of the original string, it uses spaces as the separator, then you can iterate over each item in the array as intended.

    Hope this helps =)

    ---
    |14Best regards,
    |11Ch|03rist|11ia|15n |11a|03ka |11Me|03aTLoT|11io|15N // @meatlotion:erb.pw

    |07ÄÄ |08[|10eml|08] |15ml@erb.pw |07ÄÄ |08[|10web|08] |15www.erb.pw |07ÄÄÄ¿ |07ÄÄ |08[|09fsx|08] |1521:1/158 |07ÄÄ |08[|11tqw|08] |151337:1/101 |07ÂÄÄÙ |07ÄÄ |08[|12rtn|08] |1580:774/81 |07ÄÂ |08[|14fdn|08] |152:250/5 |07ÄÄÄÙ
    |07ÄÄ |08[|10ark|08] |1510:104/2 |07ÄÙ

    --- Mystic BBS v1.12 A48 2022/07/15 (Linux/64)
    * Origin: thE qUAntUm wOrmhOlE, rAmsgAtE, uK. bbs.erb.pw (1337:1/101)
  • From deon@1337:2/101 to MeaTLoTioN on Thu Feb 2 08:49:57 2023
    Re: bash variable expansion
    By: MeaTLoTioN to All on Tue Jan 31 2023 10:16 am

    Howdy,

    I was doing some bash scripting a little while ago and while manipulating some text using some neat shortcuts I thought I'd share
    in case anyone has a need to do just this;

    Nice - useful.

    I thought I was good at bash foo - and I didnt know a few of these... ;)


    ...ëîåï
    --- SBBSecho 3.15-Linux
    * Origin: I'm playing with ANSI+videotex - wanna play too? (1337:2/101)
  • From paulie420@1337:3/129 to MeaTLoTioN on Fri Feb 3 17:02:27 2023
    This one is interesting... why do we have to create a new variable; or... what can't you just;
    echo ${string[@]^}

    Ah good question, the reason is because you need an array to be able to iterate over words in it, otherwise you'll just iterate over each character. That's why I made the array first out of the original string, it uses spaces as the separator, then you can iterate over each item in the array as intended.

    I'm understanding - thx for the deets.



    |07p|15AULIE|1142|07o
    |08.........

    --- Mystic BBS v1.12 A48 (Linux/64)
    * Origin: 2o fOr beeRS bbs>>>20ForBeers.com:1337 (1337:3/129)
  • From krull@1337:1/101 to MeaTLoTioN on Sat Feb 4 16:25:10 2023
    Hey, thanks for the tips, that was damn cool and much better than any manpages could explain :)

    [ ]
    Krull

    ... I'm not a complete idiot... Several parts are missing!

    --- Mystic BBS v1.12 A48 2022/07/15 (Linux/64)
    * Origin: thE qUAntUm wOrmhOlE, rAmsgAtE, uK. bbs.erb.pw (1337:1/101)