Skip to content

Instantly share code, notes, and snippets.

@squito
Last active March 15, 2024 13:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squito/98b3e2cf9446cf96e5efedbf3ada9f71 to your computer and use it in GitHub Desktop.
Save squito/98b3e2cf9446cf96e5efedbf3ada9f71 to your computer and use it in GitHub Desktop.
remove an arg from a command line argument in bash
#!/bin/bash
# this is a demo of how to remove an argument given with the [-arg value] notation for a specific
# [arg] (-T in this case, but easy to modify)
echo $@
echo $#
i=0
ORIGINAL_ARGS=("$@")
TRIMMED_ARGS=()
while [ $i -lt ${#ORIGINAL_ARGS[@]} ]; do
arg=${ORIGINAL_ARGS[$i]}
echo "i = $i; oa[i] = $arg"
if [ $arg == "-T" ]; then
# we want to remove both the "-T" *AND* the following arg. So we advance i here,
# and also once more outside of the for loop.
i=$((i + 1)) # careful! ((i++)) will kill your script if you use "set -e"
else
TRIMMED_ARGS+=($arg)
fi
i=$((i + 1)) # careful! ((i++)) will kill your script if you use "set -e"
done
echo "TRIMMED_ARGS = ${TRIMMED_ARGS[@]}; length = ${#TRIMMED_ARGS[@]}"
@hasii2011
Copy link

hasii2011 commented Jul 8, 2020

Later bash shell require this line

i=((i + 1))

to be

i=$((i + 1))

Otherwise, nice stuff !!

@jberryman
Copy link

You need to replace line 13 with true or move line 17 into else branch, otherwise you're incrementing the counter twice then the then branch is taken

@squito
Copy link
Author

squito commented Oct 13, 2021

@jberryman that was intentional, the purpose is to drop both the -T and the following argument, not just the -T. I've put a small comment explaining that.

But of course its just a small example, you could modify for something else.

@squito
Copy link
Author

squito commented Oct 13, 2021

Sorry I'm responding super late @hasii2011 , but thanks for the tip! I've updated

@jberryman
Copy link

@squito ah gotcha, thanks for posting this

@siddhpant
Copy link

You can use a for loop.

new_args = ()

for var in "$@"; do
	if [[ "$var" == "--what-you-want" ]]; then
		# Whatever you want
	else
		new_args+=($var)
	fi
done

@squito
Copy link
Author

squito commented Aug 11, 2022

@siddhpant the point of the example is to modify the following args based on the condition. So you remove both "-T" and the next arg (the value for the arg).

I suppose you could do that with a for loop as well, if you set some variable last_arg_was_T or something, though that seems less clear to me.

@siddhpant
Copy link

@siddhpant the point of the example is to modify the following args based on the condition. So you remove both "-T" and the next arg (the value for the arg).

@squito Correct, sorry for overlooking that.

I suppose you could do that with a for loop as well, if you set some variable last_arg_was_T or something, though that seems less clear to me.

Something like this:

#!/bin/bash

new_args=()

ignore_next_arg=false

for var in "$@"; do
	case $var in
		"-T") ;&  # Fallthrough
		"-X") ;&  # And you can go on
		"-F") ignore_next_arg=true ;;
		*)
			if [[ "$ignore_next_arg" == "true" ]]; then
				ignore_next_arg=false
			else
				new_args+=($var)
			fi
		;;
	esac
done

echo "${new_args[*]}"

@kczx3
Copy link

kczx3 commented Mar 15, 2024

I had to use the double square bracket if on line 14 and also had to wrap $arg in double quotes on line 19.

@squito
Copy link
Author

squito commented Mar 15, 2024

I had to use the double square bracket if on line 14 and also had to wrap $arg in double quotes on line 19.

oh thanks for pointing this out @kczx3 . I assume you had an argument with a space in it? I think quotes around $arg in both places would have also worked (but I admit, my bash isn't great). Probably double brackets are fine too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment