Questions tagged [arguments]

An argument is usually defined as the actual value passed to a function, procedure, or command line program.

Filter by
Sorted by
Tagged with
95
votes
8answers
33k views

What is the difference between $* and $@?

Consider the following code: foo () { echo $* } bar () { echo $@ } foo 1 2 3 4 bar 1 2 3 4 It outputs: 1 2 3 4 1 2 3 4 I am using Ksh88, but I am interested in other common ...
145
votes
7answers
397k views

Pass the output of previous command to next as an argument

I've a command that outputs data to stdout (command1 -p=aaa -v=bbb -i=4). The output line can have the following value: rate (10%) - name: value - 10Kbps I want to grep that output in order to store ...
54
votes
3answers
43k views

What defines the maximum size for a command single argument?

I was under the impression that the maximum length of a single argument was not the problem here so much as the total size of the overall argument array plus the size of the environment, which is ...
11
votes
2answers
24k views

CP: max source files number arguments for copy utility

Consider that there are countless number of files under /src/ cp /src/* /dst/ How many files cp will successfully process?
44
votes
7answers
13k views

Multiple arguments in shebang

I am wondering whether there is a general way of passing multiple options to an executable via the shebang line (#!). I use NixOS, and the first part of the shebang in any script I write is usually /...
71
votes
3answers
6k views

How does curl protect a password from appearing in ps output?

I noticed some time ago that usernames and passwords given to curl as command line arguments don't appear in ps output (although of course they may appear in your bash history). They likewise don't ...
18
votes
2answers
19k views

What's the difference between STDIN and arguments passed to command?

I could use the either form to execute the cat method: cat file_name cat < file_name The result is the same Then I want to execute man in the format of stdin man < file_name While file_name ...
9
votes
2answers
3k views

Indirectly expand variables in shell

I need to indirectly reference a variable in the bash shell. I basically want to what you can do in make by writing $($(var)). I have tried using ${$var} which would be the most straight forward ...
53
votes
5answers
104k views

Argument list too long for ls

I get the following error when trying to ls *.txt | wc -l a directory that contains many files: -bash: /bin/ls: Argument list too long Does the threshold of this "Argument list" dependent on distro ...
45
votes
7answers
4k views

Is `-` used only with cd?

cd - can switch between current dir and previous dir. It seems that I have seen - used as arguments to other commands before, though I don't remember if - means the same as with cd. I found that - ...
30
votes
2answers
2k views

Why is `sort < “$f1” ` preferred over `sort — “$f1”`, and why is this preferred over `sort “$f1”`?

From https://unix.stackexchange.com/a/458074/674 Remember to use -- when passing arbitrary arguments to commands (or use redirections where possible). So sort -- "$f1" or better sort < "$f1" ...
16
votes
2answers
33k views

how to loop through arguments in a bash script

I would like to write a bash script with unknown amount of arguments. How can I walk through these arguments and do something with them? A wrong attempt would look like this: #!/bin/bash for i in $...
111
votes
10answers
22k views

Why does argv include the program name?

Typical Unix/Linux programs accept the command line inputs as an argument count (int argc) and an argument vector (char *argv[]). The first element of argv is the program name - followed by the actual ...
73
votes
9answers
121k views

Solving “mv: Argument list too long”?

I have a folder with more than a million files that needs sorting, but I can't really do anything because mv outputs this message all the time -bash: /bin/mv: Argument list too long I'm using this ...
7
votes
2answers
3k views

UNIX-, BSD-, GNU-options in Linux's ps command. Where are they from?

In the manual for the ps command on Ubuntu there is this text: This version of ps accepts several kinds of options: 1 UNIX options, which may be grouped and must be preceded by a dash. ...
145
votes
9answers
210k views

Passing named arguments to shell scripts

Is there any easy way to pass (receive) named parameters to a shell script? For example, my_script -p_out '/some/path' -arg_1 '5' And inside my_script.sh receive them as: # I believe this ...
50
votes
1answer
13k views

When and how was the double-dash (--) introduced as an end of options delimiter in Unix/Linux?

I don't think the shell/utilities in historical Unix nor in something as "recent" as 4.4BSD supported using a double-dash(or two consecutive hyphens) as an end of options delimiter. With FreeBSD, you ...
20
votes
4answers
8k views

Why do some commands not read from their standard input?

I wonder what when we should use pipeline and when we shouldn't. Say for instance, to kill certain process which handling pdf files, the following will not work by using pipeline: ps aux | grep pdf |...
9
votes
3answers
10k views

What is a “non-option argument”?

I am trying to understand info who but completly fail at the term »non-option argument«. Can someone please explain this term to me in simple words or an example? UPDATE: from ' info who' ...
9
votes
2answers
2k views

What is a canonical way to find the actual maximum argument list length?

Following ARG_MAX, maximum length of arguments for a new process it seems like ARG_MAX is wrongly (or at least ambiguously) defined on my Mac Mini 3,1 running Ubuntu 12.04: $ getconf ARG_MAX # ...
3
votes
2answers
3k views

Why am I able to pass arguments to /usr/bin/env in this case?

I read in another answer that I'm not able to pass arguments to the interpreter than I'm giving to /usr/bin/env: Another potential problem is that the #!/usr/bin/env trick doesn't let you pass ...
5
votes
1answer
213 views

How can I create an alias that passes additional arguments?

I have an executable that I use often: /dir1/dir2/dir3/dir4/executable argv[1] argv[2] argv[3] I know that I can set an alias for the executable: alias myexecutable = '/dir1/dir2/dir3/dir4/...
6
votes
1answer
1k views

Variable with colon dash ${VAR:-}

How does "${2:-}" work in an 'if-then-else example' below? Somehow I cannot help but think that being ${2:-} it means the second argument, but I am curious what the colon(:) and dash(-) after the ...
5
votes
1answer
482 views

Executing `sh -c` script through SSH (passing arguments safely and sanely)

I suddenly realized I don't know how to execute things over SSH. I tried to do $ ssh user@server sh -c 'echo "hello"' but it outputs nothing, or rather, it outputs an empty line. If the command ...
4
votes
1answer
490 views

First argument passed to wrapper script is ignored

So I've got the following script: #!/bin/bash echo '-------------------------' echo $0 echo $1 echo $@ echo '-------------------------' exec su -- someuser -c "/tmp/elasticsearch-6.4.2/bin/...
2
votes
1answer
443 views

get first CLI argument after the options in shell scipt

I have a script I class like so gitploy up -t 2.0.0 test_repo. I pull out the "action" up right away, then I need to be able to get the test_repo before I process the options. I don't want to lose ...
2
votes
1answer
3k views

Passing arguments with spaces and quotes to a script (without quoting everything)

The following works great on the command-line: $ ffmpeg -i input.m4a -metadata 'title=Spaces and $pecial char'\''s' output.m4a How do I parameterize this command and use it in a script/function? I ...
0
votes
1answer
6k views

How many files can rm delete at once? [duplicate]

One of my friends wanted to have more logs in the /var/log/ directory, and after some time of using the system, he tried to access the folder and list it, but instead he got the following error: bash:...
0
votes
2answers
47 views

bash script passing a command as an argument

The selection should be based on submitting an argument when the script is executed. #!/bin/bash #This script echoes a text and creates a file echo "welcome. Will create a file with content in this ...
140
votes
4answers
182k views

What is the purpose of using shift in shell scripts?

I have came across this script: #! /bin/bash ...
67
votes
9answers
150k views

How do I echo just 1 column of output from ls command?

Lets say when I do ls command the output is: file1 file2 file3 file4 Is it possible to display only a certain column of output, in this case file2? I have tried the following with no success: echo ...
17
votes
2answers
2k views

How can I stream data to a program that expects to read data from a file that is given as a argument?

I have a huge gzipped file and I want a program (4s-import in this case) to read it. It takes a lot of time to first unzip the file and then call the program with the path to the file as an argument. ...
11
votes
3answers
13k views

Is there an easy way to log all commands executed, including command line arguments?

I am trying to find how to log a specific instantiation of rrdtool to see whether the path it is receiving is incorrect. I know I could wrap the executable in a shell script that would log the ...
25
votes
4answers
31k views

Is there a way to refer to multiple files in a directory without retyping the whole path? [duplicate]

In a bash shell, when I have to remove multiple files in the same directory, I currently need to do something like this: rm /some/path/file1 /some/path/file2 Is there a shorter way to write this so ...
15
votes
5answers
4k views

Hide arguments to program without source code

I need to hide some sensitive arguments to a program I am running, but I don't have access to the source code. I am also running this on a shared server so I can't use something like hidepid because I ...
7
votes
1answer
789 views

Word splitting in positional parameters

Consider the following example: IFS=: x="a :b" # three spaces echo ["$x"] # no word splitting # [a :b] # as is echo [$x] # word splitting # [a b] # four spaces Word splitting ...
17
votes
3answers
47k views

How to use arguments like $1 $2 … in a for loop? [duplicate]

I have this script which designed to download the pronounciation of the words you give it as argument: #!/bin/bash m=$# for ((i=1;i<=m;i++));do echo $i #wget https://ssl.gstatic.com/dictionary/...
26
votes
2answers
34k views

Add arguments to 'bash -c'

Let's say that I want to run a command through Bash like this: /bin/bash -c "ls -l" According to Bash man page, I could also run it like this: # don't process arguments after this one ...
15
votes
3answers
7k views

How does 'find -exec' pass file names with spaces?

If I have a directory containing some files whose names have spaces, e.g. $ ls -1 dir1 file 1 file 2 file 3 I can successfully copy all of them to another directory like this: $ find dir1 -mindepth ...
10
votes
2answers
17k views

Pass arguments to function exactly as-is

I have the following function: bar() { echo $1:$2; } I am calling this function from another function, foo. foo itself is called as follows: foo "This is" a test I want to get the following output:...
9
votes
3answers
7k views

How to pass files found by find as arguments?

First to cut off trivial but inapplicable answers: I can use neither the find+xargs trick nor its variants (like find with -exec) because I need to use few such expressions per call. I will get back ...
22
votes
2answers
12k views

How does ps know to hide passwords?

Witness: $ ps f PID TTY STAT TIME COMMAND 31509 pts/3 Ss 0:01 -bash 27266 pts/3 S+ 0:00 \_ mysql -uroot -p 25210 pts/10 Ss+ 0:00 /bin/bash 24444 pts/4 Ss 0:00 -...
15
votes
2answers
30k views

What's the difference between a flag, an option, and an argument? [closed]

ls -a (I consider -a an option) sudo -u username (-u = option, username = arg) chmod 664 my-dir (664 = option, my-dir = arg) I can't think of an example that might say "this is a flag" except perhaps ...
12
votes
3answers
1k views

Is passing a different $0 to a child bash script possible?

I know that this may sound as "not as intended by designer" but I have real life situation where a bash script I am modifying needs to call another bash script that I am not allowed to modify. That "...
7
votes
5answers
7k views

Bash script that reads filenames from a pipe or from command line args?

I want my script to read a bunch of filenames (which may have spaces) given either as a glob or from STDIN and do stuff with them. I've been able to read either way separately, but not combine them. ...
16
votes
2answers
29k views

Pass command line parameters to a program inside the shell script

As for ./script.sh arg1 [arg2 arg3 ...], the command line arguments arg1, arg2, ... can be got by $1, $2, ... But the number of arguments is NOT fixed. In the shell script, I want to pass the ...
15
votes
4answers
7k views

Why doesn't echo called as /bin/sh -c echo foo output anything?

For example, while this works: $ echo foo foo This doesn't: $ /bin/sh -c echo foo Whereas this does: $ /bin/sh -c 'echo foo; echo bar' foo bar Is there an explanation?
14
votes
2answers
9k views

How to remove a positional parameter from $@

Basically, I want to "pluck out" the first occurrence of -inf from the parameter list. (The remaining parameters will be passed along to a different command.) The script I have has the following ...
12
votes
2answers
8k views

Bash string concatenation used to build parameter list

Given this piece of bash: PARMS='-rvu' PARMS+=" --delete --exclude='.git'" echo $PARMS rsync ${PARMS} . ${TARGET} The echo shows the PARMS string as expected, no error is displayed, but rsync ...
11
votes
3answers
761 views

How can I generate arguments to another command via command substitution

Following on from: unexpected behaviour in shell command substitution I have a command which can take a huge list of arguments, some of which can legitimately contain spaces (and probably other ...