Bash command questions

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?
closed account (o3hC5Di1)
Hi there,

I found this:

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:

echo *

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:

echo "*"

http://linuxreviews.org/beginner/Bash-Scripting-Introduction-HOWTO/en/x303.html


Hope that helps.

All the best,
NwN
Last edited on
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).
Ah that definitely explains the asterisk. Makes sense now. I really wasn't too sure what was happening with it earlier. Anyway, now the dollar sign ($) is giving me grief.

echo $$


Seems to give me a prompt for entering any command. Not sure what the purpose of this is, or why it's happening.
closed account (o3hC5Di1)
Hi there,

These threads should answer that question:

http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables
http://stackoverflow.com/questions/78493/what-does-mean-in-the-shell

Hope that helps.

All the best,
NwN
Last edited on
echo $$ will give you the pid for your current process, which will be your bash shell.

If you type ps, you can see all running processes.
$$ is a shell variable that contains the PID of the running shell. There are a few of those, for example, $SHELL contains the path to the shell executable (usually /bin/bash).
closed account (o3hC5Di1)
One that I particularly like for regular cli usage is $_ (equals the last argument passed to the last command):

~$ mkdir foobar; cd $_
~/foobar$


All the best,
NwN
$? 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
	else
		echo " this script must be run as root (but you do not have 'sudo' or 'su')." >&2
		exit 1
	fi
fi

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 edited on
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?
Did it go into a file by any chance? If you put it in your .bashrc then it would be set everytime a shell was created.
I don't think it went into .bashrc. I just created it by DISPLAY=:0.0
I just created it
... ¿are you sure?
Well, I thought I was until that question. Mind telling me what I did?
And where is syscall_table.s stored?? I've been looking for awhile and can't get it. Some googling lead me to believe it is is /usr/src/linux/arch/something/kernel

I've looked into x86/kernel, ia64/kernel, and a few others and I don't see a syscall_table anywhere.

EDIT:
Ran across the find command. Ran that as su starting at root directory, and didn't even find this file. Where am I supposed to find how many system calls I have?

I'm using linux kernel version 3.2.0-23

EDIT again:
Nevermind, I found what I was looking for. Was in /arch/x86/include/asm/unistd_32.h
Last edited on
You should have used grep -F.
Well from what I ended up finding online, after 2.6, linux stopped shipping with syscall_table.s for some reason.
Topic archived. No new replies allowed.