Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to the arguments array ("$@" ) in Bash

Tags:

arrays

bash

My script takes a list of files as arguments. I want to add new argument to $@ array. For a normal array named files, appending to the array would be files+=(name_here.png). How do to I append to $@?

like image 202
user2650277 Avatar asked Nov 02 '17 17:11

user2650277


2 Answers

I would refer to @hek2mgl answer as the best array-specific answer, but if your goal is to explicitly extend $@ then go with this:

set -- "$@" '/path/to/file1' '/path/to/file2'
like image 106
Travis Clarke Avatar answered Oct 02 '22 16:10

Travis Clarke


I would copy $@ to an array and append to that:

files=( "${@}" )
files+=( name_here.png )

Then use ${files} in the script rather than ${@}.

like image 36
hek2mgl Avatar answered Oct 02 '22 15:10

hek2mgl