I’m getting a bad substitution error in this bash script – pointing to the “select key in…” line.
The issues seems to be that I’m running it in a zsh shell (it runs fine in a bash shell). But I thought that the #!/usr/bin/env bash
would address this issue. I did quite a few searches, but didn’t find an answer to this.
Here’s the script:
<code>#!/usr/bin/env bash
# Declare an associative array (hash table)
declare -A DIRS=(
["project1"]="$HOME/projects/project1"
["project2"]="$HOME/projects/project2"
["project3"]="$HOME/projects/project3"
)
# Prompt for the selection
PS3="Please enter your choice (1-${#DIRS[@]}): "
# Display the list of directories
echo "Select a directory to navigate in tmux:"
select key in "${!DIRS[@]}"; do
if [[ -n "$key" ]]; then
DIR=${DIRS[$key]}
echo "Navigating to: $DIR"
break
else
echo "Invalid selection. Please try again."
fi
done
cd "$DIR"
</code>
<code>#!/usr/bin/env bash
# Declare an associative array (hash table)
declare -A DIRS=(
["project1"]="$HOME/projects/project1"
["project2"]="$HOME/projects/project2"
["project3"]="$HOME/projects/project3"
)
# Prompt for the selection
PS3="Please enter your choice (1-${#DIRS[@]}): "
# Display the list of directories
echo "Select a directory to navigate in tmux:"
select key in "${!DIRS[@]}"; do
if [[ -n "$key" ]]; then
DIR=${DIRS[$key]}
echo "Navigating to: $DIR"
break
else
echo "Invalid selection. Please try again."
fi
done
cd "$DIR"
</code>
#!/usr/bin/env bash
# Declare an associative array (hash table)
declare -A DIRS=(
["project1"]="$HOME/projects/project1"
["project2"]="$HOME/projects/project2"
["project3"]="$HOME/projects/project3"
)
# Prompt for the selection
PS3="Please enter your choice (1-${#DIRS[@]}): "
# Display the list of directories
echo "Select a directory to navigate in tmux:"
select key in "${!DIRS[@]}"; do
if [[ -n "$key" ]]; then
DIR=${DIRS[$key]}
echo "Navigating to: $DIR"
break
else
echo "Invalid selection. Please try again."
fi
done
cd "$DIR"
4