Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass in the asterisk character '*' in bash as arguments to my C program?

Tags:

bash

escaping

Let's say I have a C program, and I run it from bash:

$ ./a.out 123 * 

The program would output all the command line arguments, but it will show these instead:

Argument 1: 123 Argument 2: a.out 

What can I do in my program to fix this?

like image 796
Yz Chong Avatar asked May 03 '10 02:05

Yz Chong


People also ask

What does * represent in Bash?

It's a space separated string of all arguments. For example, if $1 is "hello" and $2 is "world", then $* is "hello world".

What does * mean in shell script?

It means all the arguments passed to the script or function, split by word.

How do I replace a character in a string in Bash?

To replace a substring with new value in a string in Bash Script, we can use sed command. sed stands for stream editor and can be used for find and replace operation. We can specify to sed command whether to replace the first occurrence or all occurrences of the substring in the string.


1 Answers

The shell is replacing the asterisk with the name of each file in the directory.

To pass a literal asterisk, you should be able to escape it:

$ ./a.out 123 \* 
like image 99
James McNellis Avatar answered Oct 14 '22 07:10

James McNellis