Add prefix and suffix to $@ in bash











up vote
11
down vote

favorite
2












How to add suffix and prefix to $@?



If I do $PREFIX/$@/$SUFFIX, I get the prefix and the suffix only in the first parameter.










share|improve this question




















  • 1




    You will have to iterate over array and add prefix and suffix to each entry.
    – Sameer Naik
    Jul 25 '16 at 1:10










  • Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
    – dimo414
    Jul 25 '16 at 1:42






  • 1




    I'm looking for P1S P2S P3S
    – Richard
    Jul 25 '16 at 1:44















up vote
11
down vote

favorite
2












How to add suffix and prefix to $@?



If I do $PREFIX/$@/$SUFFIX, I get the prefix and the suffix only in the first parameter.










share|improve this question




















  • 1




    You will have to iterate over array and add prefix and suffix to each entry.
    – Sameer Naik
    Jul 25 '16 at 1:10










  • Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
    – dimo414
    Jul 25 '16 at 1:42






  • 1




    I'm looking for P1S P2S P3S
    – Richard
    Jul 25 '16 at 1:44













up vote
11
down vote

favorite
2









up vote
11
down vote

favorite
2






2





How to add suffix and prefix to $@?



If I do $PREFIX/$@/$SUFFIX, I get the prefix and the suffix only in the first parameter.










share|improve this question















How to add suffix and prefix to $@?



If I do $PREFIX/$@/$SUFFIX, I get the prefix and the suffix only in the first parameter.







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 25 '16 at 6:31









heemayl

20.8k12135




20.8k12135










asked Jul 25 '16 at 1:06









Richard

600522




600522








  • 1




    You will have to iterate over array and add prefix and suffix to each entry.
    – Sameer Naik
    Jul 25 '16 at 1:10










  • Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
    – dimo414
    Jul 25 '16 at 1:42






  • 1




    I'm looking for P1S P2S P3S
    – Richard
    Jul 25 '16 at 1:44














  • 1




    You will have to iterate over array and add prefix and suffix to each entry.
    – Sameer Naik
    Jul 25 '16 at 1:10










  • Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
    – dimo414
    Jul 25 '16 at 1:42






  • 1




    I'm looking for P1S P2S P3S
    – Richard
    Jul 25 '16 at 1:44








1




1




You will have to iterate over array and add prefix and suffix to each entry.
– Sameer Naik
Jul 25 '16 at 1:10




You will have to iterate over array and add prefix and suffix to each entry.
– Sameer Naik
Jul 25 '16 at 1:10












Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
– dimo414
Jul 25 '16 at 1:42




Are you looking to add a prefix an suffix to every argument, or add new prefix and suffix arguments on either side of the set of existing arguments? E.g. if your prefix is P and your suffix is S and $@ is 1 2 3 are you looking for P1S P2S P3S or P 1 2 3 S?
– dimo414
Jul 25 '16 at 1:42




1




1




I'm looking for P1S P2S P3S
– Richard
Jul 25 '16 at 1:44




I'm looking for P1S P2S P3S
– Richard
Jul 25 '16 at 1:44












3 Answers
3






active

oldest

votes

















up vote
19
down vote



accepted










I would use shell [ parameter expansion ] for this



$ set -- one two three
$ echo "$@"
one two three
$ set -- "${@/#/pre}" && set -- "${@/%/post}"
$ echo "$@"
preonepost pretwopost prethreepost


Notes




  • The # matches the beginning

  • The % matches the end

  • Using double quotes around ${@} considers each element as a separate word. so replacement happens for every positional parameter






share|improve this answer



















  • 2




    This is nice and compact. The OP should consider making this the accepted solution.
    – John1024
    Jul 25 '16 at 2:54




















up vote
8
down vote













Let's create a parameters for test purposes:



$ set -- one two three
$ echo "$@"
one two three


Now, let's use bash to add prefixes and suffixes:



$ IFS=$'n' a=($(printf "pre/%s/postn" "$@"))
$ set -- "${a[@]}"
$ echo -- "$@"
pre/one/post pre/two/post pre/three/post


Limitations: (a) since this uses newline-separated strings, it won't work if your $@ contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.



On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.



Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.



More general solution



If we don't want to make any assumptions about whitespace, we can modify "$@" with a loop:



$ a=(); for p in "$@"; do a+=("pre/$p/post"); done
$ set -- "${a[@]}"
$ echo "$@"
pre/one/post pre/two/post pre/three/post





share|improve this answer























  • Why do we need to change IFS here? I tried without it seems to work
    – Richard
    Jul 25 '16 at 1:23












  • What it the magic behind this set?
    – Richard
    Jul 25 '16 at 1:26






  • 4




    @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
    – John1024
    Jul 25 '16 at 1:35






  • 1




    @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
    – mklement0
    Jul 25 '16 at 2:16








  • 1




    @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
    – John1024
    Jul 25 '16 at 2:22


















up vote
7
down vote













Note: This is essentially a slightly more detailed version of sjam's answer.



John1024's answer is helpful, but:




  • requires a subshell (which involves a child process)

  • can result in unwanted globbing applied to the array elements.


Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:



set -- 'one' 'two' # sample input array, which will be reflected in $@

# Copy $@ to new array ${a[@]}, adding a prefix to each element.
# `/#` replaces the string that follows, up to the next `/`,
# at the *start* of each element.
# In the absence of a string, the replacement string following
# the second `/` is unconditionally placed *before* each element.
a=( "${@/#/PREFIX}" )

# Add a suffix to each element of the resulting array ${a[@]}.
# `/%` replaces the string that follows, up to the next `/`,
# at the *end* of each element.
# In the absence of a string, the replacement string following
# the second `/` is unconditionally placed *after* each element.
a=( "${a[@]/%/SUFFIX}" )

# Print the resulting array.
declare -p a


This yields:



declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'


Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38558401%2fadd-prefix-and-suffix-to-in-bash%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    19
    down vote



    accepted










    I would use shell [ parameter expansion ] for this



    $ set -- one two three
    $ echo "$@"
    one two three
    $ set -- "${@/#/pre}" && set -- "${@/%/post}"
    $ echo "$@"
    preonepost pretwopost prethreepost


    Notes




    • The # matches the beginning

    • The % matches the end

    • Using double quotes around ${@} considers each element as a separate word. so replacement happens for every positional parameter






    share|improve this answer



















    • 2




      This is nice and compact. The OP should consider making this the accepted solution.
      – John1024
      Jul 25 '16 at 2:54

















    up vote
    19
    down vote



    accepted










    I would use shell [ parameter expansion ] for this



    $ set -- one two three
    $ echo "$@"
    one two three
    $ set -- "${@/#/pre}" && set -- "${@/%/post}"
    $ echo "$@"
    preonepost pretwopost prethreepost


    Notes




    • The # matches the beginning

    • The % matches the end

    • Using double quotes around ${@} considers each element as a separate word. so replacement happens for every positional parameter






    share|improve this answer



















    • 2




      This is nice and compact. The OP should consider making this the accepted solution.
      – John1024
      Jul 25 '16 at 2:54















    up vote
    19
    down vote



    accepted







    up vote
    19
    down vote



    accepted






    I would use shell [ parameter expansion ] for this



    $ set -- one two three
    $ echo "$@"
    one two three
    $ set -- "${@/#/pre}" && set -- "${@/%/post}"
    $ echo "$@"
    preonepost pretwopost prethreepost


    Notes




    • The # matches the beginning

    • The % matches the end

    • Using double quotes around ${@} considers each element as a separate word. so replacement happens for every positional parameter






    share|improve this answer














    I would use shell [ parameter expansion ] for this



    $ set -- one two three
    $ echo "$@"
    one two three
    $ set -- "${@/#/pre}" && set -- "${@/%/post}"
    $ echo "$@"
    preonepost pretwopost prethreepost


    Notes




    • The # matches the beginning

    • The % matches the end

    • Using double quotes around ${@} considers each element as a separate word. so replacement happens for every positional parameter







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 2 at 10:29









    Community

    11




    11










    answered Jul 25 '16 at 2:09









    sjsam

    15.8k33068




    15.8k33068








    • 2




      This is nice and compact. The OP should consider making this the accepted solution.
      – John1024
      Jul 25 '16 at 2:54
















    • 2




      This is nice and compact. The OP should consider making this the accepted solution.
      – John1024
      Jul 25 '16 at 2:54










    2




    2




    This is nice and compact. The OP should consider making this the accepted solution.
    – John1024
    Jul 25 '16 at 2:54






    This is nice and compact. The OP should consider making this the accepted solution.
    – John1024
    Jul 25 '16 at 2:54














    up vote
    8
    down vote













    Let's create a parameters for test purposes:



    $ set -- one two three
    $ echo "$@"
    one two three


    Now, let's use bash to add prefixes and suffixes:



    $ IFS=$'n' a=($(printf "pre/%s/postn" "$@"))
    $ set -- "${a[@]}"
    $ echo -- "$@"
    pre/one/post pre/two/post pre/three/post


    Limitations: (a) since this uses newline-separated strings, it won't work if your $@ contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.



    On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.



    Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.



    More general solution



    If we don't want to make any assumptions about whitespace, we can modify "$@" with a loop:



    $ a=(); for p in "$@"; do a+=("pre/$p/post"); done
    $ set -- "${a[@]}"
    $ echo "$@"
    pre/one/post pre/two/post pre/three/post





    share|improve this answer























    • Why do we need to change IFS here? I tried without it seems to work
      – Richard
      Jul 25 '16 at 1:23












    • What it the magic behind this set?
      – Richard
      Jul 25 '16 at 1:26






    • 4




      @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
      – John1024
      Jul 25 '16 at 1:35






    • 1




      @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
      – mklement0
      Jul 25 '16 at 2:16








    • 1




      @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
      – John1024
      Jul 25 '16 at 2:22















    up vote
    8
    down vote













    Let's create a parameters for test purposes:



    $ set -- one two three
    $ echo "$@"
    one two three


    Now, let's use bash to add prefixes and suffixes:



    $ IFS=$'n' a=($(printf "pre/%s/postn" "$@"))
    $ set -- "${a[@]}"
    $ echo -- "$@"
    pre/one/post pre/two/post pre/three/post


    Limitations: (a) since this uses newline-separated strings, it won't work if your $@ contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.



    On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.



    Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.



    More general solution



    If we don't want to make any assumptions about whitespace, we can modify "$@" with a loop:



    $ a=(); for p in "$@"; do a+=("pre/$p/post"); done
    $ set -- "${a[@]}"
    $ echo "$@"
    pre/one/post pre/two/post pre/three/post





    share|improve this answer























    • Why do we need to change IFS here? I tried without it seems to work
      – Richard
      Jul 25 '16 at 1:23












    • What it the magic behind this set?
      – Richard
      Jul 25 '16 at 1:26






    • 4




      @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
      – John1024
      Jul 25 '16 at 1:35






    • 1




      @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
      – mklement0
      Jul 25 '16 at 2:16








    • 1




      @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
      – John1024
      Jul 25 '16 at 2:22













    up vote
    8
    down vote










    up vote
    8
    down vote









    Let's create a parameters for test purposes:



    $ set -- one two three
    $ echo "$@"
    one two three


    Now, let's use bash to add prefixes and suffixes:



    $ IFS=$'n' a=($(printf "pre/%s/postn" "$@"))
    $ set -- "${a[@]}"
    $ echo -- "$@"
    pre/one/post pre/two/post pre/three/post


    Limitations: (a) since this uses newline-separated strings, it won't work if your $@ contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.



    On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.



    Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.



    More general solution



    If we don't want to make any assumptions about whitespace, we can modify "$@" with a loop:



    $ a=(); for p in "$@"; do a+=("pre/$p/post"); done
    $ set -- "${a[@]}"
    $ echo "$@"
    pre/one/post pre/two/post pre/three/post





    share|improve this answer














    Let's create a parameters for test purposes:



    $ set -- one two three
    $ echo "$@"
    one two three


    Now, let's use bash to add prefixes and suffixes:



    $ IFS=$'n' a=($(printf "pre/%s/postn" "$@"))
    $ set -- "${a[@]}"
    $ echo -- "$@"
    pre/one/post pre/two/post pre/three/post


    Limitations: (a) since this uses newline-separated strings, it won't work if your $@ contains newlines itself. In that case, there may be another choice for IFS that would suffice. (b) This is subject to globbing. If either of these is an issue, see the more general solution below.



    On the other hand, if the positional parameters do not contain whitespace, then no change to IFS is needed.



    Also, if IFS is changed, then one may want to save IFS beforehand and restore afterward.



    More general solution



    If we don't want to make any assumptions about whitespace, we can modify "$@" with a loop:



    $ a=(); for p in "$@"; do a+=("pre/$p/post"); done
    $ set -- "${a[@]}"
    $ echo "$@"
    pre/one/post pre/two/post pre/three/post






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 10 '17 at 19:35

























    answered Jul 25 '16 at 1:15









    John1024

    74.5k86592




    74.5k86592












    • Why do we need to change IFS here? I tried without it seems to work
      – Richard
      Jul 25 '16 at 1:23












    • What it the magic behind this set?
      – Richard
      Jul 25 '16 at 1:26






    • 4




      @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
      – John1024
      Jul 25 '16 at 1:35






    • 1




      @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
      – mklement0
      Jul 25 '16 at 2:16








    • 1




      @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
      – John1024
      Jul 25 '16 at 2:22


















    • Why do we need to change IFS here? I tried without it seems to work
      – Richard
      Jul 25 '16 at 1:23












    • What it the magic behind this set?
      – Richard
      Jul 25 '16 at 1:26






    • 4




      @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
      – John1024
      Jul 25 '16 at 1:35






    • 1




      @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
      – mklement0
      Jul 25 '16 at 2:16








    • 1




      @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
      – John1024
      Jul 25 '16 at 2:22
















    Why do we need to change IFS here? I tried without it seems to work
    – Richard
    Jul 25 '16 at 1:23






    Why do we need to change IFS here? I tried without it seems to work
    – Richard
    Jul 25 '16 at 1:23














    What it the magic behind this set?
    – Richard
    Jul 25 '16 at 1:26




    What it the magic behind this set?
    – Richard
    Jul 25 '16 at 1:26




    4




    4




    @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
    – John1024
    Jul 25 '16 at 1:35




    @Richard It is only important to change IFS if you have spaces or tabs in your parameters. Otherwise, it is not needed. set is a shell builtin that, among other things, sets the positional parameters.
    – John1024
    Jul 25 '16 at 1:35




    1




    1




    @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
    – mklement0
    Jul 25 '16 at 2:16






    @John1024: a=( $(echo '*') ); declare -p a. The output from the command substitution is subject to globbing.
    – mklement0
    Jul 25 '16 at 2:16






    1




    1




    @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
    – John1024
    Jul 25 '16 at 2:22




    @mklement0 OK. Thanks. I updated the answer to note that that issue applies to the first approach.
    – John1024
    Jul 25 '16 at 2:22










    up vote
    7
    down vote













    Note: This is essentially a slightly more detailed version of sjam's answer.



    John1024's answer is helpful, but:




    • requires a subshell (which involves a child process)

    • can result in unwanted globbing applied to the array elements.


    Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:



    set -- 'one' 'two' # sample input array, which will be reflected in $@

    # Copy $@ to new array ${a[@]}, adding a prefix to each element.
    # `/#` replaces the string that follows, up to the next `/`,
    # at the *start* of each element.
    # In the absence of a string, the replacement string following
    # the second `/` is unconditionally placed *before* each element.
    a=( "${@/#/PREFIX}" )

    # Add a suffix to each element of the resulting array ${a[@]}.
    # `/%` replaces the string that follows, up to the next `/`,
    # at the *end* of each element.
    # In the absence of a string, the replacement string following
    # the second `/` is unconditionally placed *after* each element.
    a=( "${a[@]/%/SUFFIX}" )

    # Print the resulting array.
    declare -p a


    This yields:



    declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'


    Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.






    share|improve this answer



























      up vote
      7
      down vote













      Note: This is essentially a slightly more detailed version of sjam's answer.



      John1024's answer is helpful, but:




      • requires a subshell (which involves a child process)

      • can result in unwanted globbing applied to the array elements.


      Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:



      set -- 'one' 'two' # sample input array, which will be reflected in $@

      # Copy $@ to new array ${a[@]}, adding a prefix to each element.
      # `/#` replaces the string that follows, up to the next `/`,
      # at the *start* of each element.
      # In the absence of a string, the replacement string following
      # the second `/` is unconditionally placed *before* each element.
      a=( "${@/#/PREFIX}" )

      # Add a suffix to each element of the resulting array ${a[@]}.
      # `/%` replaces the string that follows, up to the next `/`,
      # at the *end* of each element.
      # In the absence of a string, the replacement string following
      # the second `/` is unconditionally placed *after* each element.
      a=( "${a[@]/%/SUFFIX}" )

      # Print the resulting array.
      declare -p a


      This yields:



      declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'


      Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.






      share|improve this answer

























        up vote
        7
        down vote










        up vote
        7
        down vote









        Note: This is essentially a slightly more detailed version of sjam's answer.



        John1024's answer is helpful, but:




        • requires a subshell (which involves a child process)

        • can result in unwanted globbing applied to the array elements.


        Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:



        set -- 'one' 'two' # sample input array, which will be reflected in $@

        # Copy $@ to new array ${a[@]}, adding a prefix to each element.
        # `/#` replaces the string that follows, up to the next `/`,
        # at the *start* of each element.
        # In the absence of a string, the replacement string following
        # the second `/` is unconditionally placed *before* each element.
        a=( "${@/#/PREFIX}" )

        # Add a suffix to each element of the resulting array ${a[@]}.
        # `/%` replaces the string that follows, up to the next `/`,
        # at the *end* of each element.
        # In the absence of a string, the replacement string following
        # the second `/` is unconditionally placed *after* each element.
        a=( "${a[@]/%/SUFFIX}" )

        # Print the resulting array.
        declare -p a


        This yields:



        declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'


        Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.






        share|improve this answer














        Note: This is essentially a slightly more detailed version of sjam's answer.



        John1024's answer is helpful, but:




        • requires a subshell (which involves a child process)

        • can result in unwanted globbing applied to the array elements.


        Fortunately, Bash parameter expansion can be applied to arrays too, which avoids these issues:



        set -- 'one' 'two' # sample input array, which will be reflected in $@

        # Copy $@ to new array ${a[@]}, adding a prefix to each element.
        # `/#` replaces the string that follows, up to the next `/`,
        # at the *start* of each element.
        # In the absence of a string, the replacement string following
        # the second `/` is unconditionally placed *before* each element.
        a=( "${@/#/PREFIX}" )

        # Add a suffix to each element of the resulting array ${a[@]}.
        # `/%` replaces the string that follows, up to the next `/`,
        # at the *end* of each element.
        # In the absence of a string, the replacement string following
        # the second `/` is unconditionally placed *after* each element.
        a=( "${a[@]/%/SUFFIX}" )

        # Print the resulting array.
        declare -p a


        This yields:



        declare -a a='([0]="PREFIXoneSUFFIX" [1]="PREFIXtwoSUFFIX")'


        Note that double-quoting the array references is crucial to protect their elements from potential word-splitting and globbing (filename expansion) - both of which are instances of shell expansions.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 12:14









        Community

        11




        11










        answered Jul 25 '16 at 1:59









        mklement0

        124k20239265




        124k20239265






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38558401%2fadd-prefix-and-suffix-to-in-bash%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents