Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a command is a shell reserved word?

Tags:

bash

shell

I am writing a bash script and I would like to verify if a string is a shell reserved word (like if, for, alias, etc...).

How can I do this?

like image 998
Eduardo Avatar asked Oct 28 '25 09:10

Eduardo


1 Answers

#!/bin/bash

string="$1"

if [[ $(type "$string" 2>&1) == "$string is a shell"* ]]; then
    echo "Keyword $string is reserved by shell"
fi
like image 199
Eugeniu Rosca Avatar answered Oct 31 '25 01:10

Eugeniu Rosca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!