How to use dash (-) with unix commands correctly? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I have just noticed that commands:
ps -T
and
ps T
give different results. For instance:
I mean lines 2 and 3 of second output.
Could someone please explain what happens here and how the dash really works in such commands (to be honest I thought options that didn't require an argument could be given without a dash).
Thanks in advance.

what happens here
From man ps with ps from procps:
This version of ps accepts several kinds of options:
1 UNIX options, which may be grouped and must be preceded by a
dash.
2. BSD options, which may be grouped and must not be used
with a dash.
3. GNU long options, which are preceded by two
dashes.
T Select all processes associated with this terminal.
-T Show threads, possibly with SPID column.
Two options with different meaning.
how the dash really works in such commands
Dash represents literally itself. - is a -, the character -. Then the program has logic, if the string that represents the argument passed to the program starts with a - character, then the logic of that program is different, then when the argument does not start with -.
o be honest I thought options that didn't require an argument could be given without a dash
Each and every program has it's own unique semantics and parsing. While there are conventions and standards and options starting with a leading - are the (almost) universal standard, there are exceptions. (For example, "traditional" tar command syntax recognizes options without a -.)
How to use dash (-) with unix commands correctly?
Consult that "command" that you want to run, as it depends on that command how it parses it's arguments.

Related

What does `args` in Linux command `ps ao args` means exactly? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
In the man page of ps, I found that -a means:
Select all processes except both session leaders (see getsid(2)) and
processes not associated with a terminal.
and -o means
User-defined format. format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired. If all column headers are empty (ps -o pid=-o comm=) then the header line will not be output. Column width will increase as needed for wide headers; this may be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality; output may be one column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options when in doubt. Use the PS_FORMAT environment variable to specify a default as desired; DefSysV and DefBSD are macros that may be used to choose the default UNIX or BSD columns.
But what does the args means in this command?
Note the args is not a placeholder for anything else, just 4 chars: a+r+g+s
args is one of the options to the -o flag, meaning "display the command with all of its arguments as a string. From the man page:
args COMMAND command with all its arguments as a string.
Modifications to the arguments may be shown.
The output in this column may contain spaces.
A process marked <defunct> is partly dead,
waiting to be fully destroyed by its parent.
Sometimes the process args will be unavailable;
when this happens, ps will instead print the
executable name in brackets. ...

What's the usages of hyphen("-") in linux shell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
We all know that when we use hyphen after the command cd, we will change to the previous working directory; and when we use hyphen after the input redirection <<, the bash will ignore the leading tabs (but not spaces) in the here script. The output from the cat command will not contain any of the leading tab characters. Is there any other usages of the hyphen character? I'm a fresh man in linux, please show more.
Lots.
I skimmed the man page, this should be pretty complete. I learned some things.
Many built-in commands use a hyphen for options, like declare.
-- declares the end of options to a shell builtin.
$- "expands to the current option flags as specified upon invocation".
-N unary minus.
N--, --N pre and post increment operators.
N - M subtraction.
N -= M subtract and assign
file1 -X file2 and -X file are file test operators
~- will expand to the value of $OLDPWD.
-X string are various string operators.
-o optname to check if an option is enabled.
-X varname various variable operators.
~-N, where N is a number, will expand to the value in the directory stack.
arg1 -op arg2 various binary comparison operators.
${parameter:-word} uses word as the default if the parameter is unset.
[a-d] in a pattern match indicates a character set range.
>&- and <&- indicates to duplicate and close a file descriptor.
<<- indicates a here document to strip leading tab characters.
%- previous job.
M-x and \M- are used to display a meta character.
C-x and \C- are used to display a control character.
Readline variables can have - in their name.
x-y are a range of words when selecting from an event.
expr1 -X expr2 boolean logic and/or test operators

How to limit terminal output of a bash command in a script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
(EDIT)
I am creating a bash program that has many fully featured options as a networking program written in python. In my program, I am using the below command and it's specific output will be used as a variable.
I am looking for a command or method to display only specific terminal output of a command. For example if, in my script, I use the command:
iwconfig wlan0
Yet all I wish to see from this command is what mode in which wlan0 is set. (i.e. Master, Managed, Monitor, etc.)
I have looked and searched all over Google and Bash specific sites and cannot find a solution. I may be overlooking something.
The grep option you're looking for is -o, or --only-matching in its long form. It outputs only text that matches the search you gave it, and nothing else. For example:
iwconfig wlan0 | grep -o 'Mode:[^ ]*'
outputs Mode:Managed on my machine. The single quotes are necessary so that the shell won't try to interpret the [, ] and * characters (with double quotes, if you happened to have a file with precisely the wrong name in your current directory, the shell might wrongly expand your parameter to the name of that file). The regular expression inside the single quotes means "the text Mode:, followed by any number of non-space characters", which is exactly what you were looking for.

dash before argument in Linux command necessary? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I found both git --version and git version are okay. They will show me the same output. However, if I try some other command, say, ls -l and ls l, only the first works.
I'd like to know how the arguments work in command line. When and where the dash or double dash before flag/arguments are necessary? Or I might be wrong with some concept when using the shell.
Thanks!
This is command dependent. There are some common commands like git or tar who have optional dashes. Most do not. You really have to read the man page to see what the command expects.
There are basically two styles for providing arguments on the command line.
The GNU style is characterized by characterized by options that look like this (-v) or like this (--verbose). The single dash sets of "short" options and the double-dash sets off "long" options. Not every short option will have a corresponding long option, or vice-versa. This syntax permits short options that do not take option arguments to be combined (for example, ls -al is equivalent to ls -a -l).
In the BSD style, one can have an option that looks like this: java -version where -version a single option, which may or may not take an argument. This style has evolved into one where complex commands have many subcommands, typically referred to as "verbs." This style is used by Apple in OSX, for example launchctl unload /path/to/some.plist ("unload" is the verb).
Hopefully this information will help you read the documentation as you go. You can find the options, and what they do, for any command, by executing, for example man ls. Note that, in some cases, there is more than one manpage for a given name. In this case, you can provide the section of the manual in which you would like to do the look up, eg man 1 crontab to see how to use the program that edits users crontabs, and man 5 crontab to see the format of an entry in these tables.
Understanding these families is useful. The authors of these commands do not want to reimplement option parsing, so they use one of the common libraries for performing this task. Thus, if you encounter a new command, once you know what family it falls into, you will have an easier time understanding the manpage.

Running files using the * command in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Let's say I wish to run all the scripts in a directory. If I do ./*.sh, which order will they run in?
Directory:
1.sh
2.sh
3.sh
To run all the scripts, you must run them separately:
for f in ./*; do
"$f"
done
The pattern will produce an alphabetically sorted list of scripts, where "alphabetical" is defined by your current locale.
Your attempt:
./*
would expand to a list of matching files, which the shell would then treat as a single command. The first script would be executed, with the remaining script names passed as arguments to the first.
They are alphabetically sorted. From the bash manual:
After word splitting, unless the -f option has been set (see The Set Builtin), Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.
However, in order to run them as you expect, you'll need to read #chepners answer (Thanks!, I must admit that I wouldn't expected that)

Resources