How do I print all arguments submitted on a command line from a bash script? - LinuxConfig.org

Question: How do I print all arguments submitted on a command line from a bash script? Answer: There are couple ways how to print bash arguments from a script. Try some scripts below to name just few. In this first script example you just print all arguments:


This is a companion discussion topic for the original entry at https://linuxconfig.org/how-do-i-print-all-arguments-submitted-on-a-command-line-from-a-bash-script

B.Max

Awesome, better than what I was looking for.
Thanks :).

Matthew -> B.Max

How can I make printf work with $@ for 2 or more arguments?

B.Max -> Matthew

No idea, I haven’t used that command in Bash.

brustadlasse

maybe something like this? I just warn you that I’m far from good at bash scripting!

if [ $# -gt 1 ]
then
# do your stuff
else
echo Missing args! 2 args is required!
fi

I wanna make script take input from user such " mkdir and execute " on bash
is there any solve

Hi Sami_remili,

Welcome to our forums.

You can get user input by reading it into a variable, something like the following:

#!/bin/bash
echo "Please provide the directory name:"
read DIRNAME

And create a directory with the provided name:

mkdir "$DIRNAME"

But I’m not sure what you mean by “mkdir and execute”, what would the script execute?

1 Like

thank you very much bro …

I mean want make script take input from user and create file by using" mkdir command line"

If your script should create a file with the name given by the user, you could use the above input reading, and use touch to create an empty file with the given name.

thank you bro its solve it

How do you keep the quotes?
For instance
./logit.sh asdfasdf “bala asdf”
returns the following and the quotes are lost
asdfasdf bala asdf

Hi Ivor_O_Connor,

Welcome to our forums.

You can escape your double quote characters with slashes:

./logit.sh asdfasdf \"bala asdf\"

Which will handle these characters as literal characters - but in this case the two words will mean two distinct arguments (as opposed to one argument that constitutes two words when you use double quotes). If you want to keep your double quotes and keep the two words as one argument, you need to add single quotes around them (and the escape slash isn’t needed):

./logit.sh asdfasdf '"bala asdf"'

That is just messed up. I was hoping for something built in that would allow me to write the exact command to file so it could simply be replayed. That’s got to be right up there with printing “hello world”. So in Python I was forced to do something like:
command=""
for x in sys.argv:
if " " in x:
command += ’ “’+x+’”’
else:
command += ’ '+x
But really hoped it was built into the shell.

I may not understand your issue clearly, but if I my goal would be to generate a script and run it, all this from a bash script, I would do something like:

echo "echo "this is the auto-generated script"" > generated.sh
echo "command1 arg1 arg2" >> generated.sh
echo "command2 "arg 1" arg2" >> generated.sh

chmod +x generated.sh
./generated.sh

Note: I used here multiple echo lines for clarity only, I could use a single one as well.

There actually is a difference between Methods 2 and 3 when dealing with quoted arguments.

Consider:

./test single1 single2 "foo bar"
Method 2
single1
single2
foo bar

Method 3
single1
single2
foo
bar

If your goal is to properly reflect passed values then ostensibly you want to respect quoted strings, so Method 3 wouldn’t pass muster.