Add prefix and suffix to $@ in bash

后端 未结 3 1180
孤街浪徒
孤街浪徒 2020-12-16 15:13

How to add suffix and prefix to $@?

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

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 16:15

    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

提交回复
热议问题