So I'm doing a lab dealing with the bash shell, and hit a question I'm not sure on. It's dealing with the asterisk (*), and I'm not sure exactly what it does. All I can find is that is matches a single string of characters. But then why does echo * just list everything in my current directory?
Double quotes are also used to prevent Bash from giving special meaning to certain metacharacters. If you want your script to output an asterisk, you might attempt to do it with this command:
But the above command outputs the names of all files and directories in the script's current working directory, which is not what you intended. This happens because the asterisk is a Bash wildcard metacharacter, and Bash replaces each word containing an asterisk with a list of matching file and directory names before executing the command. You want Bash to treat the asterisk as if it were just an ordinary character — not a metacharacter. To do this, you use double quotes to escape (or quote) the asterisk as follows:
Asterisk is used as what's called a wildcard character, it's used for pattern matching. It can match any single character, set of characters, or no character, examples:
pattern | matches | Examples
a* | anything starting with 'a' | a angry arid
*b | anything ending with 'b' | b bob dumb
a*b | anything starting with 'a' and ending with 'b' | ab absorb
*a* | anything with 'a' in the middle | a far foal fealty
* | any string, including empty strings |
In BASH it's usually used for matching filenames. An asterisk by itself is replaced by every filename in the current directory. An asterisk with other characters around it is replaced by any filenames that match, so for example, '.*' would only match hidden files on UNIX (ones which start with '.') while *.* would only match files that have a '.' (useful for when you only want files which have extensions).
$? is probably my favourite; it gets the return value of the last process. If I write a script that has to be run as root, I like to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
script="$(basename $0)"if [[ $EUID -ne 0 ]]; then # if user ID != 0 (root)
echo -n "$script: You are not root, " >&2 # no endline (-n), redirect stdout to stderr (>&2)
if [[ -x "$(which sudo)" ]]; then # if 'sudo' executable exists
echo "trying sudo" >&2
sudo "$0" # run this script as root
exit $? # exit with sudo exit code
elif [[ -x "$(which su)" ]]; then # if 'su' executable exists
echo "trying su"
su -c "$0" # run this script as root
exit $? # exit with su exit code
echo " this script must be run as root (but you do not have 'sudo' or 'su')." >&2
EUID means "effective user ID". The user ID is any positive integer, but 0 is the root user. My EUID on Cygwin 1000 apparently. Numbers are used instead of strings because it's a lot faster.
Last question here. This was dealing with creating a shell variable, and accessing it from different instances of bash. Anyway, I created a variable in my first shell, accessed it fine, then created another instance of bash. From here, I could still access it just fine even though I never exported the variable (ASFAIK you have to export the variable to access it from other processes, right?). This seemed weird to me, is there a reason why this happened?