Extract parameters before last parameter in "$@"
bash, parameters
Solution
To remove the last item from the array you could use something like this:
#!/bin/bash
length=$(($#-1))
array=${@:1:$length}
echo $array
Even shorter way:
array=${@:1:$#-1}
But arays are a Bashism, try avoid using them :(.
Problem
I'm trying to create a Bash script that will extract the last parameter given from the command line into a variable to be used elsewhere. Here's the script I'm working on: ``` #!/bin/bash # compact - archive and compact file/folder(s) eval LAST=\$$# FILES="$@" NAME=$LAST # Usage - display usage if no parameters are given if [[ -z $NAME ]]; then echo "compact <file> <folder>... <compressed-name>.tar.gz" exit fi # Check if an archive name has been given if [[ -f $NAME ]]; then echo "File exists or you forgot to enter a filename. Exiting." exit fi tar -czvpf "$NAME".tar.gz $FILES ``` Since the first parameters could be of any number, I have to find a way to extract the last parameter, (e.g. compact file.a file.b file.d files-a-b-d.tar.gz). As it is now the archive name will be included in the files to compact. Is there a way to do this?