user2650277
user2650277

Reputation: 6739

How to append to the arguments array ("$@" ) in 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 $@?

Upvotes: 16

Views: 7108

Answers (2)

Travis Clarke
Travis Clarke

Reputation: 6671

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'

Upvotes: 17

hek2mgl
hek2mgl

Reputation: 157990

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

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

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

Upvotes: 8

Related Questions