Add prefix and suffix to $@ in bash












11














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
















11














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














11












11








11


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.9k12135




20.9k12135










asked Jul 25 '16 at 1:06









RichardRichard

599522




599522








  • 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


















19














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





















8














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



















7














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',
    autoActivateHeartbeat: false,
    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









    19














    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


















    19














    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
















    19












    19








    19






    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 '18 at 10:29









    Community

    11




    11










    answered Jul 25 '16 at 2:09









    sjsamsjsam

    15.9k33170




    15.9k33170








    • 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















    8














    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
















    8














    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














    8












    8








    8






    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









    John1024John1024

    74.9k86694




    74.9k86694












    • 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











    7














    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




























      7














      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


























        7












        7








        7






        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









        mklement0mklement0

        127k20241269




        127k20241269






























            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

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?