Works on the command line, doesn't work in a bash script...?

So I'm doing a bit of reworking and improving an existing script, and I ran into something I can't figure out.

This line in the script:
sudo zsysctl show --full | grep -C 2 Kernel | grep $chosenpool | tac | grep $chosenpool -m $kernelstrim | awk -F '[_ ]+' /${chosenpool}/''' {print $5}'| xargs -n1 zsysctl state remove -s
... where kernelstrim is the number of ZFS snapshots to clean up and...
... where chosenpool is, in our case, either bpool or rpool.

... works just fine when run from the command line like this:
First, set up your variables:
kernelstrim=1 {enter}
chosenpool=bpool {enter}
Then paste that line above and press enter... and it works.

But if I put it into the bash script, it doesn't work. The problem is in the variable highlighted below:
sudo zsysctl show --full | grep -C 2 Kernel | grep $chosenpool | tac | grep $chosenpool -m $kernelstrim | awk -F '[_ ]+' /${chosenpool}/''' {print $5}'| xargs -n1 zsysctl state remove -s

I'm obviously not getting that bit right. Anybody got any help?

Ah, I got it... that line is correct, but it was affected by other code which under certain circumstances made it return the wrong thing.

Ok, here's the code. It's not perfected by any means, but it's already saved me once... in removing all the unnecessary packages from dpkg using apt, each removal triggered a snapshot, and the snapshots built up until there was no more space for snapshots and hard drive space used was 12.3 GB (yes, as I removed stuff, hard drive space used went up!).

Using my zfsflush.sh code and the code below, I was able to whittle it down to only one large snapshot (as you remove a snapshot, its contents get incorporated into other snapshots with redundancies removed), total hard drive space used is only 5.15 GB and the system is now saving snapshots again.

#!/bin/bash
# Manual zsysctl gc
clear
garbagespace=
kernelscount=
kernelstrim=
chosenpool=rpool

check () {
	echo "These are your pools:"
	sudo zpool list -Ho name
	read -p "Which ZFS pool would you like to work on? " chosenpool
	garbagespace=$(sudo zpool list -o capacity $chosenpool | awk '{if (NR==2) {print substr($1, 1, length($1)-1)}}')
	kernelscount=$(zsysctl show --full | grep -c -C 2 Kernel)
	kernelscount=$((kernelscount-1))
	clear
	echo "Your" $chosenpool "is" $garbagespace% "full."
	echo "You have $kernelscount kernel snapshots available to be removed."
	echo ""
}

empty () {
	garbagespace=$(zpool list | awk -F '[% ]+' '/$chosenpool/ {print $8}')
	kernelscount=$(zsysctl show --full | grep -c -C 2 Kernel)
	kernelscount=$((kernelscount-1))
	if [[ $garbagespace -gt 50 ]]
	then
	echo "How many snapshots would you like to trim?"
	echo "0 - $kernelscount"
			while :; do
			read kernelstrim
			[[ $kernelstrim =~ ^[0-9]+$ ]] || { echo "Enter a valid number!"; continue; }
			if ((kernelstrim >= 0 && kernelstrim <= (kernelscount))); then
			break
			else
			echo "Number out of range, try again!"
			fi
			done
		if [[ $kernelstrim -gt 0 ]]
		then
		clear
		echo ""
		echo "Collecting Garbage"
		echo ""
		sudo zsysctl show --full | grep -C 2 Kernel | grep $chosenpool | tac | grep $chosenpool -m $kernelstrim | awk -F '[_ ]+' /${chosenpool}/''' {print $5}'| xargs -n1 zsysctl state remove -s
		echo ""
		echo "Done."
		echo ""
		elif [[ $kernelstrim = 0 ]]
		then
		clear
		echo ""
		echo "Cancelled" >&2
		echo ""
		fi
	else
	clear
	echo $chosenpool "is less than 50% full"
	echo "Do you still want to trim snapshots?"
	read trimanswer
		if [[ $trimanswer = yes ]] || [[ $trimanswer = y ]]
		then
		clear
		echo "How many snapshots would you like to trim?"
		echo "0 - $kernelscount"
				while :; do
				read kernelstrim
				[[ $kernelstrim =~ ^[0-9]+$ ]] || { echo "Enter a valid number!"; continue; }
				if ((kernelstrim >= 0 && kernelstrim <= (kernelscount))); then
				break
				else
				echo "Number out of range, try again!"
				fi
				done
			if [[ $kernelstrim -gt 0 ]]
			then
			clear
			echo ""
			echo "Collecting Garbage"
			echo ""
			sudo zsysctl show --full | grep -C 2 Kernel | grep $chosenpool | tac | grep $chosenpool -m $kernelstrim | awk -F '[_ ]+' /${chosenpool}/''' {print $5}'| xargs -n1 zsysctl state remove -s
			echo ""
			echo "Done."
			echo ""
			elif [[ $kernelstrim = 0 ]]
			then
			clear
			echo ""
			echo "Cancelled"
			echo ""
			fi
		fi
	fi
}

clear
echo "This script:"
echo "A) Allows you to check space used by snapshots on each pool."
echo "B) Allows you to check how many kernel shapshots you have."
echo "C) Allows you to delete all but one kernel snapshot"
while true; do
	options=("Check Garbage" "Empty Garbage" "Quit")
	echo "Choose an option: "
	select opt in "${options[@]}"; do
		case $REPLY in
			1) check; break ;;
			2) empty; break ;;
			3) break 2 ;;
			*) echo "Invalid Option!" >&2
			esac
	done
done

echo "Exiting..."
sleep 3