QuestionId
stringlengths
3
8
QuestionTitle
stringlengths
15
149
QuestionScore
int64
1
27.2k
QuestionCreatedUtc
stringdate
2008-08-01 18:33:08
2025-06-16 04:59:48
QuestionBodyHtml
stringlengths
40
28.2k
QuestionViewCount
int64
78
16.5M
QuestionOwnerUserId
float64
4
25.2M
QuestionLastActivityUtc
stringdate
2008-09-08 19:26:01
2026-01-24 22:40:25
AcceptedAnswerId
int64
266
79.7M
AnswerScore
int64
2
30.1k
AnswerCreatedUtc
stringdate
2008-08-01 23:40:28
2025-06-16 05:23:39
AnswerOwnerUserId
float64
1
29.5M
AnswerLastActivityUtc
stringdate
2008-08-01 23:40:28
2026-01-23 17:46:09
QuestionLink
stringlengths
31
36
AnswerLink
stringlengths
31
36
AnswerBodyHtml
stringlengths
35
33k
AnswerBodyPreview
stringlengths
35
400
AllTagIdsCsv
stringlengths
2
37
AllTagNamesCsv
stringlengths
2
106
MergedQuestion
stringlengths
74
28.4k
quality_filter
stringclasses
10 values
unique_id
int64
0
29k
tag_major_lang
stringclasses
10 values
MergedQuestion_md
stringlengths
60
28.1k
AnswerBodyHtml_md
stringlengths
14
29.2k
19622198
What does 'set -e' mean in a Bash script?
1,195
2013-10-27 19:06:32
<p>I'm studying the content of this <code>preinst</code> file that the script executes before that package is unpacked from its <a href="https://en.wikipedia.org/wiki/Debian" rel="noreferrer">Debian</a> archive (<code>.deb</code>) file.</p> <p>The script has the following code:</p> <pre><code>#!/bin/bash set -e # Autom...
808,651
1,833,945
2026-01-18 23:31:21
19,622,569
1,214
2013-10-27 19:39:10
465,183
2026-01-18 23:31:21
https://stackoverflow.com/q/19622198
https://stackoverflow.com/a/19622569
<p>From <code>help set</code> and <a href="https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html#index-set" rel="nofollow noreferrer"><em>Bash Reference Documentation: The Set Builtin</em></a>:</p> <blockquote> <p><code>-e Exit immediately if a command exits with a non-zero status.</code></p> </block...
<p>From <code>help set</code> and <a href="https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html#index-set" rel="nofollow noreferrer"><em>Bash Reference Documentation: The Set Builtin</em></a>:</p> <blockquote> <p><code>-e Exit immediately if a command exits with a non-zero status.</code></p> </block...
58, 387, 390, 10327
bash, linux, sh, shell
<h1>What does 'set -e' mean in a Bash script?</h1> <p>I'm studying the content of this <code>preinst</code> file that the script executes before that package is unpacked from its <a href="https://en.wikipedia.org/wiki/Debian" rel="noreferrer">Debian</a> archive (<code>.deb</code>) file.</p> <p>The script has the follow...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
116
bash
# What does 'set -e' mean in a Bash script? I'm studying the content of this `preinst` file that the script executes before that package is unpacked from its [Debian](https://en.wikipedia.org/wiki/Debian) archive (`.deb`) file. The script has the following code: ``` #!/bin/bash set -e # Automatically added by dh_ins...
From `help set` and [*Bash Reference Documentation: The Set Builtin*](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html#index-set): > `-e Exit immediately if a command exits with a non-zero status.` But it's considered bad practice by some (Bash FAQ and [IRC](https://en.wikipedia.org/wiki/Intern...
2157554
How to handle command-line arguments in PowerShell
658
2010-01-28 20:01:10
<p>What is the "best" way to handle command-line arguments?</p> <p>It seems like there are several answers on what the "best" way is and as a result I am stuck on how to handle something as simple as:</p> <pre><code>script.ps1 /n name /d domain </code></pre> <p>AND</p> <pre><code>script.ps1 /d domain /n name. </cod...
984,179
261,317
2024-08-20 11:04:35
2,157,625
1,213
2010-01-28 20:13:40
239,599
2022-04-22 17:47:56
https://stackoverflow.com/q/2157554
https://stackoverflow.com/a/2157625
<p>You are reinventing the wheel. Normal PowerShell scripts have parameters starting with <code>-</code>, like <code>script.ps1 -server http://devserver</code></p> <p>Then you handle them in a <code>param</code> section (note that this <strong>must</strong> begin at the first non-commented line in your script).</p> <p>...
<p>You are reinventing the wheel. Normal PowerShell scripts have parameters starting with <code>-</code>, like <code>script.ps1 -server http://devserver</code></p> <p>Then you handle them in a <code>param</code> section (note that this <strong>must</strong> begin at the first non-commented line in your script).</p> <p>...
526, 31134
command-line-arguments, powershell
<h1>How to handle command-line arguments in PowerShell</h1> <p>What is the "best" way to handle command-line arguments?</p> <p>It seems like there are several answers on what the "best" way is and as a result I am stuck on how to handle something as simple as:</p> <pre><code>script.ps1 /n name /d domain </code></pre>...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
117
bash
# How to handle command-line arguments in PowerShell What is the "best" way to handle command-line arguments? It seems like there are several answers on what the "best" way is and as a result I am stuck on how to handle something as simple as: ``` script.ps1 /n name /d domain ``` AND ``` script.ps1 /d domain /n na...
You are reinventing the wheel. Normal PowerShell scripts have parameters starting with `-`, like `script.ps1 -server http://devserver` Then you handle them in a `param` section (note that this **must** begin at the first non-commented line in your script). You can also assign default values to your params, read them ...
22727107
How to find the last field using 'cut'
575
2014-03-29 04:43:37
<p><em>Without</em> using <code>sed</code> or <code>awk</code>, <em>only</em> <code>cut</code>, how do I get the last field when the number of fields are unknown or change with every line?</p>
508,886
3,262,003
2024-10-19 14:50:01
22,727,211
1,211
2014-03-29 04:58:11
2,554,537
2020-02-06 21:02:43
https://stackoverflow.com/q/22727107
https://stackoverflow.com/a/22727211
<p>You could try something like this:</p> <pre><code>echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev </code></pre> <p><strong>Explanation</strong></p> <ul> <li><code>rev</code> reverses "maps.google.com" to be <code>moc.elgoog.spam</code></li> <li><code>cut</code> uses dot (ie '.') as the delimiter, and chooses ...
<p>You could try something like this:</p> <pre><code>echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev </code></pre> <p><strong>Explanation</strong></p> <ul> <li><code>rev</code> reverses "maps.google.com" to be <code>moc.elgoog.spam</code></li> <li><code>cut</code> uses dot (ie '.') as the delimiter, and chooses ...
58, 387, 4257
bash, cut, linux
<h1>How to find the last field using 'cut'</h1> <p><em>Without</em> using <code>sed</code> or <code>awk</code>, <em>only</em> <code>cut</code>, how do I get the last field when the number of fields are unknown or change with every line?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
118
bash
# How to find the last field using 'cut' *Without* using `sed` or `awk`, *only* `cut`, how do I get the last field when the number of fields are unknown or change with every line?
You could try something like this: ``` echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev ``` **Explanation** - `rev` reverses "maps.google.com" to be `moc.elgoog.spam` - `cut` uses dot (ie '.') as the delimiter, and chooses the first field, which is `moc` - lastly, we reverse it again to get `com`
806906
How do I test if a variable is a number in Bash?
931
2009-04-30 13:30:16
<p>I just can't figure out how do I make sure an argument passed to my script is a number or not.</p> <p>All I want to do is something like this:</p> <pre><code>test *isnumber* $1 &amp;&amp; VAR=$1 || echo "need a number" </code></pre> <p>Any help?</p>
1,121,549
802
2025-12-30 15:05:10
806,923
1,199
2009-04-30 13:32:20
14,122
2018-08-26 00:40:36
https://stackoverflow.com/q/806906
https://stackoverflow.com/a/806923
<p>One approach is to use a regular expression, like so:</p> <pre><code>re='^[0-9]+$' if ! [[ $yournumber =~ $re ]] ; then echo "error: Not a number" &gt;&amp;2; exit 1 fi </code></pre> <p>If the value is not necessarily an integer, consider amending the regex appropriately; for instance:</p> <pre><code>^[0-9]+([...
<p>One approach is to use a regular expression, like so:</p> <pre><code>re='^[0-9]+$' if ! [[ $yournumber =~ $re ]] ; then echo "error: Not a number" &gt;&amp;2; exit 1 fi </code></pre> <p>If the value is not necessarily an integer, consider amending the regex appropriately; for instance:</p> <pre><code>^[0-9]+([...
387, 390
bash, shell
<h1>How do I test if a variable is a number in Bash?</h1> <p>I just can't figure out how do I make sure an argument passed to my script is a number or not.</p> <p>All I want to do is something like this:</p> <pre><code>test *isnumber* $1 &amp;&amp; VAR=$1 || echo "need a number" </code></pre> <p>Any help?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
119
bash
# How do I test if a variable is a number in Bash? I just can't figure out how do I make sure an argument passed to my script is a number or not. All I want to do is something like this: ``` test *isnumber* $1 && VAR=$1 || echo "need a number" ``` Any help?
One approach is to use a regular expression, like so: ``` re='^[0-9]+$' if ! [[ $yournumber =~ $re ]] ; then echo "error: Not a number" >&2; exit 1 fi ``` If the value is not necessarily an integer, consider amending the regex appropriately; for instance: ``` ^[0-9]+([.][0-9]+)?$ ``` ...or, to handle numbers wit...
2016894
How can I split a large text file into smaller files with an equal number of lines?
773
2010-01-06 22:40:39
<p>I've got a large (by number of lines) plain text file that I'd like to split into smaller files, also by number of lines. So if my file has around 2M lines, I'd like to split it up into 10 files that contain 200k lines, or 100 files that contain 20k lines (plus one file with the remainder; being evenly divisible do...
843,493
217,332
2025-05-20 03:15:31
2,016,918
1,191
2010-01-06 22:44:37
61,974
2024-12-22 18:35:27
https://stackoverflow.com/q/2016894
https://stackoverflow.com/a/2016918
<p>Have a look at the <a href="https://www.gnu.org/software/coreutils/manual/html_node/split-invocation.html" rel="noreferrer">split command</a>:</p> <p>For version: <code>(GNU coreutils) 8.32</code></p> <pre class="lang-none prettyprint-override"><code>$ split --help Usage: split [OPTION]... [FILE [PREFIX]] Output pie...
<p>Have a look at the <a href="https://www.gnu.org/software/coreutils/manual/html_node/split-invocation.html" rel="noreferrer">split command</a>:</p> <p>For version: <code>(GNU coreutils) 8.32</code></p> <pre class="lang-none prettyprint-override"><code>$ split --help Usage: split [OPTION]... [FILE [PREFIX]] Output pie...
34, 387, 5310
bash, file, unix
<h1>How can I split a large text file into smaller files with an equal number of lines?</h1> <p>I've got a large (by number of lines) plain text file that I'd like to split into smaller files, also by number of lines. So if my file has around 2M lines, I'd like to split it up into 10 files that contain 200k lines, or ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
120
bash
# How can I split a large text file into smaller files with an equal number of lines? I've got a large (by number of lines) plain text file that I'd like to split into smaller files, also by number of lines. So if my file has around 2M lines, I'd like to split it up into 10 files that contain 200k lines, or 100 files ...
Have a look at the [split command](https://www.gnu.org/software/coreutils/manual/html_node/split-invocation.html): For version: `(GNU coreutils) 8.32` ``` $ split --help Usage: split [OPTION]... [FILE [PREFIX]] Output pieces of FILE to PREFIXaa, PREFIXab, ...; default size is 1000 lines, and default PREFIX is 'x'. W...
4565700
How to specify the private SSH-key to use when executing shell command on Git?
1,916
2010-12-30 19:42:01
<p>A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (<code>git</code>) command from the local computer.</p> <p>Basically like this:</p> <pre><code>git clone git@github.com:TheUser/TheProject.git -key &quot;/home/christoffer/ssh_keys/theuser&quot; </code></pre> <p...
2,260,713
160,574
2025-06-29 09:49:17
4,565,746
1,179
2010-12-30 19:48:28
33,006
2016-11-22 20:42:26
https://stackoverflow.com/q/4565700
https://stackoverflow.com/a/4565746
<p>Something like this should work (suggested by orip):</p> <pre><code>ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git' </code></pre> <p>if you prefer subshells, you could try the following (though it is more fragile):</p> <pre><code>ssh-agent $(ssh-add /somewhere/yourkey; gi...
<p>Something like this should work (suggested by orip):</p> <pre><code>ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git' </code></pre> <p>if you prefer subshells, you could try the following (though it is more fragile):</p> <pre><code>ssh-agent $(ssh-add /somewhere/yourkey; gi...
119, 386, 387, 390
bash, git, shell, ssh
<h1>How to specify the private SSH-key to use when executing shell command on Git?</h1> <p>A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (<code>git</code>) command from the local computer.</p> <p>Basically like this:</p> <pre><code>git clone git@github.com:The...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
121
bash
# How to specify the private SSH-key to use when executing shell command on Git? A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (`git`) command from the local computer. Basically like this: ``` git clone git@github.com:TheUser/TheProject.git -key "/home/chri...
Something like this should work (suggested by orip): ``` ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git' ``` if you prefer subshells, you could try the following (though it is more fragile): ``` ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.gi...
20796200
How to loop over files in directory and change path and add suffix to filename
883
2013-12-27 06:49:34
<p>I need to write a script that starts my program with different arguments. I start my program with:</p> <p><code>./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]</code>.</p> <p>Here is the pseudocode for what I want to do:</p> <pre class="lang-bash prettyprint-override"><code>for each filename in /Data do for in...
1,476,105
2,918,918
2025-07-09 14:52:44
20,796,617
1,178
2013-12-27 07:23:33
89,817
2021-10-17 00:50:34
https://stackoverflow.com/q/20796200
https://stackoverflow.com/a/20796617
<p>A couple of notes first: when you use <code>Data/data1.txt</code> as an argument, should it really be <code>/Data/data1.txt</code> (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming <code>/Data/data1.txt</code> and .txt files only:</p> <pre...
<p>A couple of notes first: when you use <code>Data/data1.txt</code> as an argument, should it really be <code>/Data/data1.txt</code> (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming <code>/Data/data1.txt</code> and .txt files only:</p> <pre...
387, 1062, 2531, 3889
bash, filenames, for-loop, glob
<h1>How to loop over files in directory and change path and add suffix to filename</h1> <p>I need to write a script that starts my program with different arguments. I start my program with:</p> <p><code>./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]</code>.</p> <p>Here is the pseudocode for what I want to do:</p> ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
122
bash
# How to loop over files in directory and change path and add suffix to filename I need to write a script that starts my program with different arguments. I start my program with: `./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]`. Here is the pseudocode for what I want to do: ``` for each filename in /Data do ...
A couple of notes first: when you use `Data/data1.txt` as an argument, should it really be `/Data/data1.txt` (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming `/Data/data1.txt` and .txt files only: ``` #!/bin/bash for filename in /Data/*.txt...
1378274
In a Bash script, how can I exit the entire script if a certain condition occurs?
1,003
2009-09-04 09:51:52
<p>I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests.</p> <p>Is there a way I can do this without wrapping the entire script inside of a while loop and using breaks? Something like a <strong>d...
1,156,716
39,036
2024-07-15 09:22:36
1,378,284
1,171
2009-09-04 09:53:53
149,530
2024-07-15 09:22:36
https://stackoverflow.com/q/1378274
https://stackoverflow.com/a/1378284
<p>Try this statement:</p> <pre><code>exit 1 </code></pre> <p>Replace <code>1</code> with appropriate error codes. See also <a href="/questions/1101957/are-there-any-standard-exit-status-codes-in-linux">Exit Codes With Special Meanings</a>.</p>
<p>Try this statement:</p> <pre><code>exit 1 </code></pre> <p>Replace <code>1</code> with appropriate error codes. See also <a href="/questions/1101957/are-there-any-standard-exit-status-codes-in-linux">Exit Codes With Special Meanings</a>.</p>
387, 531, 10021, 10871
bash, exit, exit-code, scripting
<h1>In a Bash script, how can I exit the entire script if a certain condition occurs?</h1> <p>I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests.</p> <p>Is there a way I can do this without wra...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
123
bash
# In a Bash script, how can I exit the entire script if a certain condition occurs? I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests. Is there a way I can do this without wrapping the entire...
Try this statement: ``` exit 1 ``` Replace `1` with appropriate error codes. See also [Exit Codes With Special Meanings](/questions/1101957/are-there-any-standard-exit-status-codes-in-linux).
6022384
Bash tool to get nth line from a file
910
2011-05-16 19:33:42
<p>Is there a "canonical" way of doing that? I've been using <code>head -n | tail -1</code> which does the trick, but I've been wondering if there's a Bash tool that specifically extracts a line (or a range of lines) from a file.</p> <p>By "canonical" I mean a program whose main function is doing that.</p>
764,927
754,336
2025-07-29 13:56:39
6,022,431
1,168
2011-05-16 19:38:33
548,225
2025-07-28 20:51:04
https://stackoverflow.com/q/6022384
https://stackoverflow.com/a/6022431
<p><code>head</code> and pipe with <code>tail</code> will be slow for a huge file. I would suggest <code>sed</code> like this:</p> <pre><code>sed 'NUMq;d' file </code></pre> <p>Where <code>NUM</code> is the number of the line you want to print; so, for example, <code>sed '10q;d' file</code> to print the 10th line of <c...
<p><code>head</code> and pipe with <code>tail</code> will be slow for a huge file. I would suggest <code>sed</code> like this:</p> <pre><code>sed 'NUMq;d' file </code></pre> <p>Where <code>NUM</code> is the number of the line you want to print; so, for example, <code>sed '10q;d' file</code> to print the 10th line of <c...
34, 387, 390, 990, 5282
awk, bash, sed, shell, unix
<h1>Bash tool to get nth line from a file</h1> <p>Is there a "canonical" way of doing that? I've been using <code>head -n | tail -1</code> which does the trick, but I've been wondering if there's a Bash tool that specifically extracts a line (or a range of lines) from a file.</p> <p>By "canonical" I mean a program who...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
124
bash
# Bash tool to get nth line from a file Is there a "canonical" way of doing that? I've been using `head -n | tail -1` which does the trick, but I've been wondering if there's a Bash tool that specifically extracts a line (or a range of lines) from a file. By "canonical" I mean a program whose main function is doing t...
`head` and pipe with `tail` will be slow for a huge file. I would suggest `sed` like this: ``` sed 'NUMq;d' file ``` Where `NUM` is the number of the line you want to print; so, for example, `sed '10q;d' file` to print the 10th line of `file`. Explanation: `NUMq` will quit immediately when the line number is `NUM`....
8748831
When do we need curly braces around shell variables?
1,034
2012-01-05 19:54:03
<p>In shell scripts, when do we use <code>{}</code> when expanding variables?</p> <p>For example, I have seen the following:</p> <pre><code>var=10 # Declare variable echo "${var}" # One use of the variable echo "$var" # Another use of the variable </code></pre> <p>Is there a significant difference, or is i...
428,212
1,066,402
2025-02-09 15:51:07
8,748,880
1,164
2012-01-05 19:56:34
166,749
2025-02-09 15:51:07
https://stackoverflow.com/q/8748831
https://stackoverflow.com/a/8748880
<p>In your particular example, it makes no difference. However, the <code>{}</code> in <code>${}</code> are useful if you want to expand the variable <code>foo</code> in the string</p> <pre><code>&quot;${foo}bar&quot; </code></pre> <p>since <code>&quot;$foobar&quot;</code> would instead expand the variable identified b...
<p>In your particular example, it makes no difference. However, the <code>{}</code> in <code>${}</code> are useful if you want to expand the variable <code>foo</code> in the string</p> <pre><code>&quot;${foo}bar&quot; </code></pre> <p>since <code>&quot;$foobar&quot;</code> would instead expand the variable identified b...
367, 387, 390, 14116, 19749
bash, curly-braces, shell, syntax, variable-expansion
<h1>When do we need curly braces around shell variables?</h1> <p>In shell scripts, when do we use <code>{}</code> when expanding variables?</p> <p>For example, I have seen the following:</p> <pre><code>var=10 # Declare variable echo "${var}" # One use of the variable echo "$var" # Another use of the variabl...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
125
bash
# When do we need curly braces around shell variables? In shell scripts, when do we use `{}` when expanding variables? For example, I have seen the following: ``` var=10 # Declare variable echo "${var}" # One use of the variable echo "$var" # Another use of the variable ``` Is there a significant differen...
In your particular example, it makes no difference. However, the `{}` in `${}` are useful if you want to expand the variable `foo` in the string ``` "${foo}bar" ``` since `"$foobar"` would instead expand the variable identified by `foobar`. Curly braces are also unconditionally required when: - expanding array elem...
84882
sudo echo "something" >> /etc/privilegedFile doesn't work
754
2008-09-17 16:09:00
<p>This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux.</p> <p>There are a lot of times when I just want to append something to <code>/etc/hosts</code> or a similar file but end up not being able to because both <code>&gt;</code> and <code>&gt;&gt;</code> are not allo...
337,876
9,908
2020-09-24 14:41:48
550,808
1,160
2009-02-15 12:34:20
37,664
2019-08-12 14:49:09
https://stackoverflow.com/q/84882
https://stackoverflow.com/a/550808
<p>Use <code>tee --append</code> or <code>tee -a</code>.</p> <pre><code>echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list </code></pre> <p>Make sure to avoid quotes inside quotes.</p> <p>To avoid printing data back to the console, redirect the output to /dev/null.</p> <pre><code>echo 'deb blah ... blah' ...
<p>Use <code>tee --append</code> or <code>tee -a</code>.</p> <pre><code>echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list </code></pre> <p>Make sure to avoid quotes inside quotes.</p> <p>To avoid printing data back to the console, redirect the output to /dev/null.</p> <pre><code>echo 'deb blah ... blah' ...
359, 387, 390, 531, 1674
bash, permissions, scripting, shell, sudo
<h1>sudo echo "something" >> /etc/privilegedFile doesn't work</h1> <p>This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux.</p> <p>There are a lot of times when I just want to append something to <code>/etc/hosts</code> or a similar file but end up not being able to be...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
126
bash
# sudo echo "something" >> /etc/privilegedFile doesn't work This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux. There are a lot of times when I just want to append something to `/etc/hosts` or a similar file but end up not being able to because both `>` and `>>` are...
Use `tee --append` or `tee -a`. ``` echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list ``` Make sure to avoid quotes inside quotes. To avoid printing data back to the console, redirect the output to /dev/null. ``` echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list > /dev/null ``` Remember about...
733824
How can I run a shell script on a Unix console or Mac terminal?
625
2009-04-09 11:36:26
<p>How can I run a shell script on a Unix console or Mac terminal?</p> <p>I know it, forget it and relearn it again. It is time to write it down.</p>
1,247,834
4,975
2025-11-27 18:22:41
733,901
1,138
2009-04-09 11:58:50
58,803
2025-11-27 18:22:41
https://stackoverflow.com/q/733824
https://stackoverflow.com/a/733901
<p>To run a non-executable <a href="https://en.wikipedia.org/wiki/Bourne_shell" rel="nofollow noreferrer">Bourne shell</a> (executable <code>sh</code>) script, use:</p> <pre class="lang-none prettyprint-override"><code>sh myscript </code></pre> <p>To run a non-executable Bash script, use:</p> <pre class="lang-none pret...
<p>To run a non-executable <a href="https://en.wikipedia.org/wiki/Bourne_shell" rel="nofollow noreferrer">Bourne shell</a> (executable <code>sh</code>) script, use:</p> <pre class="lang-none prettyprint-override"><code>sh myscript </code></pre> <p>To run a non-executable Bash script, use:</p> <pre class="lang-none pret...
34, 58, 369, 387, 390
bash, linux, macos, shell, unix
<h1>How can I run a shell script on a Unix console or Mac terminal?</h1> <p>How can I run a shell script on a Unix console or Mac terminal?</p> <p>I know it, forget it and relearn it again. It is time to write it down.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
127
bash
# How can I run a shell script on a Unix console or Mac terminal? How can I run a shell script on a Unix console or Mac terminal? I know it, forget it and relearn it again. It is time to write it down.
To run a non-executable [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell) (executable `sh`) script, use: ``` sh myscript ``` To run a non-executable Bash script, use: ``` bash myscript ``` To start an executable (which is any file with executable permission); you just specify it by its path: ``` /foo/bar ...
3096259
Bash command to sum a column of numbers
516
2010-06-22 18:58:59
<p>I want a bash command that I can pipe into that will sum a column of numbers. I just want a quick one liner that will do something essentially like this:</p> <pre><code>cat FileWithColumnOfNumbers.txt | sum </code></pre>
450,235
54,777
2025-10-11 13:13:47
3,096,575
1,133
2010-06-22 19:47:56
430,749
2021-04-30 15:40:21
https://stackoverflow.com/q/3096259
https://stackoverflow.com/a/3096575
<h1>Using existing file:</h1> <pre><code>paste -sd+ infile | bc </code></pre> <h1>Using stdin:</h1> <pre><code>&lt;cmd&gt; | paste -sd+ | bc </code></pre> <p>Edit: With some <em>paste</em> implementations you need to be more explicit when reading from <em>stdin</em>:</p> <p><code>&lt;cmd&gt; | paste -sd+ - | bc</code><...
<h1>Using existing file:</h1> <pre><code>paste -sd+ infile | bc </code></pre> <h1>Using stdin:</h1> <pre><code>&lt;cmd&gt; | paste -sd+ | bc </code></pre> <p>Edit: With some <em>paste</em> implementations you need to be more explicit when reading from <em>stdin</em>:</p> <p><code>&lt;cmd&gt; | paste -sd+ - | bc</code><...
387
bash
<h1>Bash command to sum a column of numbers</h1> <p>I want a bash command that I can pipe into that will sum a column of numbers. I just want a quick one liner that will do something essentially like this:</p> <pre><code>cat FileWithColumnOfNumbers.txt | sum </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
128
bash
# Bash command to sum a column of numbers I want a bash command that I can pipe into that will sum a column of numbers. I just want a quick one liner that will do something essentially like this: ``` cat FileWithColumnOfNumbers.txt | sum ```
# Using existing file: ``` paste -sd+ infile | bc ``` # Using stdin: ``` <cmd> | paste -sd+ | bc ``` Edit: With some *paste* implementations you need to be more explicit when reading from *stdin*: `<cmd> | paste -sd+ - | bc` **Options used**: > -s (serial) - merges all the lines into a single line > > -d - use a...
9332802
How to write a bash script that takes optional input arguments?
796
2012-02-17 17:32:52
<p>I want my script to be able to take an optional input, </p> <p>e.g. currently my script is </p> <pre><code>#!/bin/bash somecommand foo </code></pre> <p>but I would like it to say:</p> <pre><code>#!/bin/bash somecommand [ if $1 exists, $1, else, foo ] </code></pre>
528,275
513,006
2024-10-25 19:20:26
9,333,006
1,125
2012-02-17 17:50:19
561,879
2015-01-31 20:45:53
https://stackoverflow.com/q/9332802
https://stackoverflow.com/a/9333006
<p>You could use the default-value syntax:</p> <pre><code>somecommand ${1:-foo} </code></pre> <p>The above will, as described in <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">Bash Reference Manual - 3.5.3 Shell Parameter Expansion</a> [emphasis mine]:</p> <...
<p>You could use the default-value syntax:</p> <pre><code>somecommand ${1:-foo} </code></pre> <p>The above will, as described in <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">Bash Reference Manual - 3.5.3 Shell Parameter Expansion</a> [emphasis mine]:</p> <...
387, 2313, 10355
arguments, bash, parameter-passing
<h1>How to write a bash script that takes optional input arguments?</h1> <p>I want my script to be able to take an optional input, </p> <p>e.g. currently my script is </p> <pre><code>#!/bin/bash somecommand foo </code></pre> <p>but I would like it to say:</p> <pre><code>#!/bin/bash somecommand [ if $1 exists, $1, ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
129
bash
# How to write a bash script that takes optional input arguments? I want my script to be able to take an optional input, e.g. currently my script is ``` #!/bin/bash somecommand foo ``` but I would like it to say: ``` #!/bin/bash somecommand [ if $1 exists, $1, else, foo ] ```
You could use the default-value syntax: ``` somecommand ${1:-foo} ``` The above will, as described in [Bash Reference Manual - 3.5.3 Shell Parameter Expansion](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion) [emphasis mine]: > If parameter is unset *or null*, the expansion of word is ...
2035193
How to run a PowerShell script
1,105
2010-01-09 22:19:10
<p>How do I run a PowerShell script?</p> <ul> <li>I have a script named myscript.ps1</li> <li>I have all the necessary frameworks installed</li> <li>I set that <a href="https://stackoverflow.com/questions/10635/why-dont-my-powershell-scripts-run">execution policy</a> thing</li> <li>I have followed the instructions on ...
3,609,787
187,606
2025-04-26 19:27:11
2,035,209
1,113
2010-01-09 22:24:43
13,302
2022-03-31 17:59:23
https://stackoverflow.com/q/2035193
https://stackoverflow.com/a/2035209
<p>Prerequisites:</p> <ul> <li>You need to be able to run PowerShell as an administrator</li> <li>You need to set your PowerShell execution policy to a permissive value or be able to bypass it</li> </ul> <p>Steps:</p> <ol> <li><p>Launch Windows PowerShell as an Administrator, and wait for the <code>PS&gt;</code> prompt...
<p>Prerequisites:</p> <ul> <li>You need to be able to run PowerShell as an administrator</li> <li>You need to set your PowerShell execution policy to a permissive value or be able to bypass it</li> </ul> <p>Steps:</p> <ol> <li><p>Launch Windows PowerShell as an Administrator, and wait for the <code>PS&gt;</code> prompt...
64, 526, 531
powershell, scripting, windows
<h1>How to run a PowerShell script</h1> <p>How do I run a PowerShell script?</p> <ul> <li>I have a script named myscript.ps1</li> <li>I have all the necessary frameworks installed</li> <li>I set that <a href="https://stackoverflow.com/questions/10635/why-dont-my-powershell-scripts-run">execution policy</a> thing</li> ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
130
bash
# How to run a PowerShell script How do I run a PowerShell script? - I have a script named myscript.ps1 - I have all the necessary frameworks installed - I set that [execution policy](https://stackoverflow.com/questions/10635/why-dont-my-powershell-scripts-run) thing - I have followed the instructions on [this MSDN h...
Prerequisites: - You need to be able to run PowerShell as an administrator - You need to set your PowerShell execution policy to a permissive value or be able to bypass it Steps: 1. Launch Windows PowerShell as an Administrator, and wait for the `PS>` prompt to appear 2. Navigate within PowerShell to the directory w...
5130847
Running multiple commands in one line in shell
549
2011-02-27 01:36:10
<p>Say I have a file <code>/templates/apple</code> and I want to</p> <ol> <li>put it in two different places and then</li> <li>remove the original.</li> </ol> <p>So, <code>/templates/apple</code> will be copied to <code>/templates/used</code> AND <code>/templates/inuse</code> and then after that I’d like to remove th...
784,259
460,121
2025-03-01 19:23:36
5,130,889
1,104
2011-02-27 01:44:56
412,080
2024-06-30 22:42:01
https://stackoverflow.com/q/5130847
https://stackoverflow.com/a/5130889
<p>You are using <code>|</code> (pipe) to direct the output of a command into another command. What you are looking for is <code>&amp;&amp;</code> operator to execute the next command only if the previous one succeeded:</p> <pre><code>cp /templates/apple /templates/used &amp;&amp; cp /templates/apple /templates/inuse &...
<p>You are using <code>|</code> (pipe) to direct the output of a command into another command. What you are looking for is <code>&amp;&amp;</code> operator to execute the next command only if the previous one succeeded:</p> <pre><code>cp /templates/apple /templates/used &amp;&amp; cp /templates/apple /templates/inuse &...
387, 390
bash, shell
<h1>Running multiple commands in one line in shell</h1> <p>Say I have a file <code>/templates/apple</code> and I want to</p> <ol> <li>put it in two different places and then</li> <li>remove the original.</li> </ol> <p>So, <code>/templates/apple</code> will be copied to <code>/templates/used</code> AND <code>/template...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
131
bash
# Running multiple commands in one line in shell Say I have a file `/templates/apple` and I want to 1. put it in two different places and then 2. remove the original. So, `/templates/apple` will be copied to `/templates/used` AND `/templates/inuse` and then after that I’d like to remove the original. Is `cp` the be...
You are using `|` (pipe) to direct the output of a command into another command. What you are looking for is `&&` operator to execute the next command only if the previous one succeeded: ``` cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple ``` Or ``` cp /templates/ap...
3915040
How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?
775
2010-10-12 13:12:09
<p>Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it?</p> <p>Usage case: I'm in directory <code>/a/b</code> and I'd like to print the full path to file <code>c</code> on the command-line so that I can easily paste it into another program: <code>/a/b/c</code...
698,349
314,345
2024-04-15 09:52:30
3,915,075
1,103
2010-10-12 13:16:11
176,922
2020-08-24 23:52:56
https://stackoverflow.com/q/3915040
https://stackoverflow.com/a/3915075
<p>Use <a href="https://man7.org/linux/man-pages/man1/realpath.1.html" rel="noreferrer"><code>realpath</code></a></p> <pre><code>$ realpath example.txt /home/username/example.txt </code></pre>
<p>Use <a href="https://man7.org/linux/man-pages/man1/realpath.1.html" rel="noreferrer"><code>realpath</code></a></p> <pre><code>$ realpath example.txt /home/username/example.txt </code></pre>
387, 390, 6268
bash, path, shell
<h1>How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?</h1> <p>Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it?</p> <p>Usage case: I'm in directory <code>/a/b</code> and I'd like to print the full path to file <code>c</code> on the command...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
132
bash
# How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)? Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it? Usage case: I'm in directory `/a/b` and I'd like to print the full path to file `c` on the command-line so that I can easily paste it in...
Use [`realpath`](https://man7.org/linux/man-pages/man1/realpath.1.html) ``` $ realpath example.txt /home/username/example.txt ```
981601
Colorized grep -- viewing the entire file with highlighted matches
710
2009-06-11 14:55:29
<p>I find <code>grep</code>'s <code>--color=always</code> flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.</p> <p>I'd really like to <code>cat</code> ...
354,153
109,549
2024-12-29 10:54:43
981,831
1,096
2009-06-11 15:30:58
88,894
2021-04-25 12:35:22
https://stackoverflow.com/q/981601
https://stackoverflow.com/a/981831
<p>Here are some ways to do it:</p> <pre><code>grep --color 'pattern\|$' file grep --color -E 'pattern|$' file egrep --color 'pattern|$' file </code></pre> <p>The <code>|</code> symbol is the OR operator. Either escape it using <code>\</code> or tell grep that the search text has to be interpreted as regular expression...
<p>Here are some ways to do it:</p> <pre><code>grep --color 'pattern\|$' file grep --color -E 'pattern|$' file egrep --color 'pattern|$' file </code></pre> <p>The <code>|</code> symbol is the OR operator. Either escape it using <code>\</code> or tell grep that the search text has to be interpreted as regular expression...
387, 390, 1271, 6439
bash, colors, grep, shell
<h1>Colorized grep -- viewing the entire file with highlighted matches</h1> <p>I find <code>grep</code>'s <code>--color=always</code> flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
133
bash
# Colorized grep -- viewing the entire file with highlighted matches I find `grep`'s `--color=always` flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could....
Here are some ways to do it: ``` grep --color 'pattern\|$' file grep --color -E 'pattern|$' file egrep --color 'pattern|$' file ``` The `|` symbol is the OR operator. Either escape it using `\` or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the `egrep` command ins...
692000
How do I write standard error to a file while using "tee" with a pipe?
826
2009-03-28 01:53:04
<p>I know how to use <code>tee</code> to write the output (<a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="noreferrer">standard output</a>) of <code>aaa.sh</code> to <code>bbb.out</code>, while still displaying it in the terminal:</p> <pre class="lang-none prettyprint-override...
434,764
82,856
2025-09-02 10:42:20
692,407
1,094
2009-03-28 07:54:59
58,803
2022-04-12 22:38:55
https://stackoverflow.com/q/692000
https://stackoverflow.com/a/692407
<p>I'm assuming you want to still see standard error and standard output on the terminal. You could go for <a href="https://stackoverflow.com/questions/692000/how-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipe/692009#692009">Josh Kelley's answer</a>, but I find keeping a <code>tail</code> around in th...
<p>I'm assuming you want to still see standard error and standard output on the terminal. You could go for <a href="https://stackoverflow.com/questions/692000/how-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipe/692009#692009">Josh Kelley's answer</a>, but I find keeping a <code>tail</code> around in th...
34, 58, 387, 22055
bash, linux, tee, unix
<h1>How do I write standard error to a file while using "tee" with a pipe?</h1> <p>I know how to use <code>tee</code> to write the output (<a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="noreferrer">standard output</a>) of <code>aaa.sh</code> to <code>bbb.out</code>, while sti...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
134
bash
# How do I write standard error to a file while using "tee" with a pipe? I know how to use `tee` to write the output ([standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29)) of `aaa.sh` to `bbb.out`, while still displaying it in the terminal: ``` ./aaa.sh | tee bbb.out ``` How...
I'm assuming you want to still see standard error and standard output on the terminal. You could go for [Josh Kelley's answer](https://stackoverflow.com/questions/692000/how-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipe/692009#692009), but I find keeping a `tail` around in the background which outputs...
21297853
How to determine SSL cert expiration date from a PEM encoded certificate?
602
2014-01-23 01:55:19
<p>If I have the actual file and a Bash shell in Mac or Linux, how can I query the cert file for when it will expire? Not a web site, but actually the certificate file itself, assuming I have the csr, key, pem and chain files.</p>
959,584
2,117,603
2025-04-10 08:19:41
21,297,927
1,094
2014-01-23 02:01:49
1,899,640
2015-07-31 17:21:40
https://stackoverflow.com/q/21297853
https://stackoverflow.com/a/21297927
<p>With <code>openssl</code>:</p> <pre><code>openssl x509 -enddate -noout -in file.pem </code></pre> <p>The output is on the form:</p> <pre><code>notAfter=Nov 3 22:23:50 2014 GMT </code></pre> <p>Also see <a href="https://stackoverflow.com/a/31718838/1899640">MikeW's answer</a> for how to easily check whether the ...
<p>With <code>openssl</code>:</p> <pre><code>openssl x509 -enddate -noout -in file.pem </code></pre> <p>The output is on the form:</p> <pre><code>notAfter=Nov 3 22:23:50 2014 GMT </code></pre> <p>Also see <a href="https://stackoverflow.com/a/31718838/1899640">MikeW's answer</a> for how to easily check whether the ...
58, 387, 641, 1999, 5954
bash, certificate, linux, openssl, ssl
<h1>How to determine SSL cert expiration date from a PEM encoded certificate?</h1> <p>If I have the actual file and a Bash shell in Mac or Linux, how can I query the cert file for when it will expire? Not a web site, but actually the certificate file itself, assuming I have the csr, key, pem and chain files.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
135
bash
# How to determine SSL cert expiration date from a PEM encoded certificate? If I have the actual file and a Bash shell in Mac or Linux, how can I query the cert file for when it will expire? Not a web site, but actually the certificate file itself, assuming I have the csr, key, pem and chain files.
With `openssl`: ``` openssl x509 -enddate -noout -in file.pem ``` The output is on the form: ``` notAfter=Nov 3 22:23:50 2014 GMT ``` Also see [MikeW's answer](https://stackoverflow.com/a/31718838/1899640) for how to easily check whether the certificate has expired or not, or whether it will within a certain time ...
23929235
Multi-line string with extra space (preserved indentation)
744
2014-05-29 08:44:38
<p>I want to write some pre-defined texts to a file with the following:</p> <pre><code>text="this is line one\n this is line two\n this is line three" echo -e $text &gt; filename </code></pre> <p>I'm expecting something like this:</p> <pre><code>this is line one this is line two this is line three </code></pre> <p...
1,000,043
1,925,083
2025-11-23 21:04:07
23,930,212
1,078
2014-05-29 09:37:12
2,794,538
2025-11-23 21:03:18
https://stackoverflow.com/q/23929235
https://stackoverflow.com/a/23930212
<p><a href="https://en.wikipedia.org/wiki/Here_document" rel="nofollow noreferrer">Heredoc</a> sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program, like <em><a href="https://en.wikipedia.org/wiki/Ex_%28text_editor%29" rel="nofollow noreferrer">ex</a></em> or <e...
<p><a href="https://en.wikipedia.org/wiki/Here_document" rel="nofollow noreferrer">Heredoc</a> sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program, like <em><a href="https://en.wikipedia.org/wiki/Ex_%28text_editor%29" rel="nofollow noreferrer">ex</a></em> or <e...
139, 387, 390, 13824
bash, echo, shell, string
<h1>Multi-line string with extra space (preserved indentation)</h1> <p>I want to write some pre-defined texts to a file with the following:</p> <pre><code>text="this is line one\n this is line two\n this is line three" echo -e $text &gt; filename </code></pre> <p>I'm expecting something like this:</p> <pre><code>th...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
136
bash
# Multi-line string with extra space (preserved indentation) I want to write some pre-defined texts to a file with the following: ``` text="this is line one\n this is line two\n this is line three" echo -e $text > filename ``` I'm expecting something like this: ``` this is line one this is line two this is line th...
[Heredoc](https://en.wikipedia.org/wiki/Here_document) sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program, like *[ex](https://en.wikipedia.org/wiki/Ex_%28text_editor%29)* or *[cat](https://en.wikipedia.org/wiki/Cat_(Unix))* ``` cat << EndOfMessage This is lin...
15113413
How do I concatenate strings and variables in PowerShell?
1,000
2013-02-27 13:34:02
<p>Suppose I have the following snippet:</p> <pre class="lang-powershell prettyprint-override"><code>$assoc = New-Object PSObject -Property @{ Id = 42 Name = &quot;Slim Shady&quot; Owner = &quot;Eminem&quot; } Write-Host $assoc.Id + &quot; - &quot; + $assoc.Name + &quot; - &quot; + $assoc.Owner </code...
2,549,490
1,389,663
2024-01-10 14:09:28
15,113,467
1,074
2013-02-27 13:37:03
311,712
2024-01-10 14:09:28
https://stackoverflow.com/q/15113413
https://stackoverflow.com/a/15113467
<pre><code>Write-Host &quot;$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)&quot; </code></pre> <p>See the <a href="https://www.microsoft.com/en-us/download/details.aspx?id=36389" rel="noreferrer">Windows PowerShell Language Specification Version 3.0</a>, p25, sub-expressions expansion.</p>
<pre><code>Write-Host &quot;$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)&quot; </code></pre> <p>See the <a href="https://www.microsoft.com/en-us/download/details.aspx?id=36389" rel="noreferrer">Windows PowerShell Language Specification Version 3.0</a>, p25, sub-expressions expansion.</p>
526, 7792
powershell, string-concatenation
<h1>How do I concatenate strings and variables in PowerShell?</h1> <p>Suppose I have the following snippet:</p> <pre class="lang-powershell prettyprint-override"><code>$assoc = New-Object PSObject -Property @{ Id = 42 Name = &quot;Slim Shady&quot; Owner = &quot;Eminem&quot; } Write-Host $assoc.Id + &quot; ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
137
bash
# How do I concatenate strings and variables in PowerShell? Suppose I have the following snippet: ``` $assoc = New-Object PSObject -Property @{ Id = 42 Name = "Slim Shady" Owner = "Eminem" } Write-Host $assoc.Id + " - " + $assoc.Name + " - " + $assoc.Owner ``` I'd expect this snippet to show: > `4...
``` Write-Host "$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)" ``` See the [Windows PowerShell Language Specification Version 3.0](https://www.microsoft.com/en-us/download/details.aspx?id=36389), p25, sub-expressions expansion.
36724209
How to disable bells/beeps in the WSL terminal on Windows 10
718
2016-04-19 16:26:44
<p>How do I disable visual and audio bells/beeps in Windows Subsystem for Linux (WSL) on Windows 10?</p>
282,036
3,943,047
2025-06-03 19:48:41
36,726,662
1,074
2016-04-19 18:32:14
3,943,047
2021-12-29 18:17:38
https://stackoverflow.com/q/36724209
https://stackoverflow.com/a/36726662
<ol> <li><p>To disable the beep in <strong>bash</strong> you need to uncomment (or add if not already there) the line <code>set bell-style none</code> in your <code>/etc/inputrc</code> file.</p> <p><em>Note:</em> Since it is a protected file you need to be a privileged user to edit it (i.e. launch your text editor with...
<ol> <li><p>To disable the beep in <strong>bash</strong> you need to uncomment (or add if not already there) the line <code>set bell-style none</code> in your <code>/etc/inputrc</code> file.</p> <p><em>Note:</em> Since it is a protected file you need to be a privileged user to edit it (i.e. launch your text editor with...
387, 107329, 119526
bash, windows-10, windows-subsystem-for-linux
<h1>How to disable bells/beeps in the WSL terminal on Windows 10</h1> <p>How do I disable visual and audio bells/beeps in Windows Subsystem for Linux (WSL) on Windows 10?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
138
bash
# How to disable bells/beeps in the WSL terminal on Windows 10 How do I disable visual and audio bells/beeps in Windows Subsystem for Linux (WSL) on Windows 10?
1. To disable the beep in **bash** you need to uncomment (or add if not already there) the line `set bell-style none` in your `/etc/inputrc` file. *Note:* Since it is a protected file you need to be a privileged user to edit it (i.e. launch your text editor with something like `sudo <editor> /etc/inputrc`). 2. To d...
637827
Redirect stderr and stdout in Bash
961
2009-03-12 09:14:05
<p>I want to redirect both <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)" rel="noreferrer">standard output</a> and <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)" rel="noreferrer">standard error</a> of a process to a single file. How do I do that in Ba...
1,180,724
63,051
2025-10-20 21:44:30
637,839
1,065
2009-03-12 09:17:25
66,692
2025-10-20 21:40:48
https://stackoverflow.com/q/637827
https://stackoverflow.com/a/637839
<p>Take a look <a href="https://tldp.org/LDP/abs/html/io-redirection.html" rel="noreferrer">here</a>. It should be:</p> <pre><code>yourcommand &amp;&gt; filename </code></pre> <p>It redirects both standard output and standard error to file <em>filename</em>.</p>
<p>Take a look <a href="https://tldp.org/LDP/abs/html/io-redirection.html" rel="noreferrer">here</a>. It should be:</p> <pre><code>yourcommand &amp;&gt; filename </code></pre> <p>It redirects both standard output and standard error to file <em>filename</em>.</p>
387, 390, 4867, 19156, 26698
bash, io-redirection, shell, stderr, stdout
<h1>Redirect stderr and stdout in Bash</h1> <p>I want to redirect both <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)" rel="noreferrer">standard output</a> and <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)" rel="noreferrer">standard error</a> of a proc...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
139
bash
# Redirect stderr and stdout in Bash I want to redirect both [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) and [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)) of a process to a single file. How do I do that in Bash?
Take a look [here](https://tldp.org/LDP/abs/html/io-redirection.html). It should be: ``` yourcommand &> filename ``` It redirects both standard output and standard error to file *filename*.
13799789
Expansion of variables inside single quotes in a command in Bash
696
2012-12-10 11:10:13
<p>I want to run a command from a <em>bash script</em> which has single quotes and some other commands inside the single quotes and a variable.</p> <p>e.g. <code>repo forall -c '....$variable'</code></p> <p>In this format, <code>$</code> is escaped and the variable is not expanded.</p> <p>I tried the following varia...
722,106
1,879,748
2025-06-02 02:59:01
13,802,438
1,062
2012-12-10 14:00:30
1,073,695
2019-06-19 14:19:27
https://stackoverflow.com/q/13799789
https://stackoverflow.com/a/13802438
<p>Inside single quotes everything is preserved literally, without exception.</p> <p>That means you have to close the quotes, insert something, and then re-enter again.</p> <pre><code>'before'"$variable"'after' 'before'"'"'after' 'before'\''after' </code></pre> <p>Word concatenation is simply done by juxtaposition. ...
<p>Inside single quotes everything is preserved literally, without exception.</p> <p>That means you have to close the quotes, insert something, and then re-enter again.</p> <pre><code>'before'"$variable"'after' 'before'"'"'after' 'before'\''after' </code></pre> <p>Word concatenation is simply done by juxtaposition. ...
276, 387, 390, 430
bash, quotes, shell, variables
<h1>Expansion of variables inside single quotes in a command in Bash</h1> <p>I want to run a command from a <em>bash script</em> which has single quotes and some other commands inside the single quotes and a variable.</p> <p>e.g. <code>repo forall -c '....$variable'</code></p> <p>In this format, <code>$</code> is esc...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
140
bash
# Expansion of variables inside single quotes in a command in Bash I want to run a command from a *bash script* which has single quotes and some other commands inside the single quotes and a variable. e.g. `repo forall -c '....$variable'` In this format, `$` is escaped and the variable is not expanded. I tried the ...
Inside single quotes everything is preserved literally, without exception. That means you have to close the quotes, insert something, and then re-enter again. ``` 'before'"$variable"'after' 'before'"'"'after' 'before'\''after' ``` Word concatenation is simply done by juxtaposition. As you can verify, each of the abo...
3258243
Check if pull needed in Git
769
2010-07-15 17:32:27
<p>How do I check whether the remote repository has changed and I need to pull?</p> <p>Now I use this simple script:</p> <pre><code>git pull --dry-run | grep -q -v 'Already up-to-date.' &amp;&amp; changed=1 </code></pre> <p>But it is rather heavy.</p> <p>Is there a better way? The ideal solution would check all the...
522,058
234,780
2025-01-01 00:48:27
3,278,427
1,045
2010-07-19 04:18:54
381,619
2022-03-14 17:04:09
https://stackoverflow.com/q/3258243
https://stackoverflow.com/a/3278427
<p>First use <a href="https://git-scm.com/docs/git-remote" rel="noreferrer"><code>git remote update</code></a>, to bring your remote refs up to date. Then you can do one of several things, such as:</p> <ol> <li><p><code>git status -uno</code> will tell you whether the branch you are tracking is ahead, behind or has div...
<p>First use <a href="https://git-scm.com/docs/git-remote" rel="noreferrer"><code>git remote update</code></a>, to bring your remote refs up to date. Then you can do one of several things, such as:</p> <ol> <li><p><code>git status -uno</code> will tell you whether the branch you are tracking is ahead, behind or has div...
119, 387, 390
bash, git, shell
<h1>Check if pull needed in Git</h1> <p>How do I check whether the remote repository has changed and I need to pull?</p> <p>Now I use this simple script:</p> <pre><code>git pull --dry-run | grep -q -v 'Already up-to-date.' &amp;&amp; changed=1 </code></pre> <p>But it is rather heavy.</p> <p>Is there a better way? T...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
141
bash
# Check if pull needed in Git How do I check whether the remote repository has changed and I need to pull? Now I use this simple script: ``` git pull --dry-run | grep -q -v 'Already up-to-date.' && changed=1 ``` But it is rather heavy. Is there a better way? The ideal solution would check all the remote branches, ...
First use [`git remote update`](https://git-scm.com/docs/git-remote), to bring your remote refs up to date. Then you can do one of several things, such as: 1. `git status -uno` will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same. 2. ...
8789729
How to zero pad a sequence of integers in bash so that all have the same width?
647
2012-01-09 14:12:18
<p>I need to loop some values,</p> <pre><code>for i in $(seq $first $last) do does something here done </code></pre> <p>For <code>$first</code> and <code>$last</code>, I need it to be of fixed length 5. So if the input is <code>1</code>, I need to add zeros in front such that it becomes <code>00001</code>. It loops...
440,335
1,138,842
2024-10-26 13:39:32
8,789,815
1,044
2012-01-09 14:17:43
3,171
2012-01-09 14:26:12
https://stackoverflow.com/q/8789729
https://stackoverflow.com/a/8789815
<p>In your specific case though it's probably easiest to use the <code>-f</code> flag to <code>seq</code> to get it to format the numbers as it outputs the list. For example:</p> <pre><code>for i in $(seq -f "%05g" 10 15) do echo $i done </code></pre> <p>will produce the following output:</p> <pre><code>00010 000...
<p>In your specific case though it's probably easiest to use the <code>-f</code> flag to <code>seq</code> to get it to format the numbers as it outputs the list. For example:</p> <pre><code>for i in $(seq -f "%05g" 10 15) do echo $i done </code></pre> <p>will produce the following output:</p> <pre><code>00010 000...
387, 3496, 9282
bash, numbers, padding
<h1>How to zero pad a sequence of integers in bash so that all have the same width?</h1> <p>I need to loop some values,</p> <pre><code>for i in $(seq $first $last) do does something here done </code></pre> <p>For <code>$first</code> and <code>$last</code>, I need it to be of fixed length 5. So if the input is <code...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
142
bash
# How to zero pad a sequence of integers in bash so that all have the same width? I need to loop some values, ``` for i in $(seq $first $last) do does something here done ``` For `$first` and `$last`, I need it to be of fixed length 5. So if the input is `1`, I need to add zeros in front such that it becomes `00...
In your specific case though it's probably easiest to use the `-f` flag to `seq` to get it to format the numbers as it outputs the list. For example: ``` for i in $(seq -f "%05g" 10 15) do echo $i done ``` will produce the following output: ``` 00010 00011 00012 00013 00014 00015 ``` More generally, `bash` has `p...
4111475
How to do a logical OR operation for integer comparison in shell scripting?
593
2010-11-06 01:48:33
<p>I am trying to do a simple condition check, but it doesn't seem to work.</p> <p>If <code>$#</code> is equal to <code>0</code> or is greater than <code>1</code> then say hello.</p> <p>I have tried the following syntax with no success:</p> <pre><code>if [ "$#" == 0 -o "$#" &gt; 1 ] ; then echo "hello" fi if [ "$#...
1,038,318
170,365
2021-02-23 13:04:53
4,111,510
1,033
2010-11-06 02:03:12
148,217
2018-04-26 10:40:20
https://stackoverflow.com/q/4111475
https://stackoverflow.com/a/4111510
<p>This should work:</p> <pre><code>#!/bin/bash if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then echo "hello" fi </code></pre> <p>I'm not sure if this is different in other shells but if you wish to use &lt;, >, you need to put them inside double parenthesis like so: </p> <pre><code>if (("$#" &gt; 1)) ... </code></p...
<p>This should work:</p> <pre><code>#!/bin/bash if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then echo "hello" fi </code></pre> <p>I'm not sure if this is different in other shells but if you wish to use &lt;, >, you need to put them inside double parenthesis like so: </p> <pre><code>if (("$#" &gt; 1)) ... </code></p...
34, 387, 2773, 10327
bash, if-statement, sh, unix
<h1>How to do a logical OR operation for integer comparison in shell scripting?</h1> <p>I am trying to do a simple condition check, but it doesn't seem to work.</p> <p>If <code>$#</code> is equal to <code>0</code> or is greater than <code>1</code> then say hello.</p> <p>I have tried the following syntax with no succe...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
143
bash
# How to do a logical OR operation for integer comparison in shell scripting? I am trying to do a simple condition check, but it doesn't seem to work. If `$#` is equal to `0` or is greater than `1` then say hello. I have tried the following syntax with no success: ``` if [ "$#" == 0 -o "$#" > 1 ] ; then echo "hell...
This should work: ``` #!/bin/bash if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then echo "hello" fi ``` I'm not sure if this is different in other shells but if you wish to use <, >, you need to put them inside double parenthesis like so: ``` if (("$#" > 1)) ... ```
3349105
How can I set the current working directory to the directory of the script in Bash?
851
2010-07-28 00:43:29
<p>I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in.</p> <p>The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.</p>
688,407
229,792
2025-03-11 07:14:36
3,355,423
1,029
2010-07-28 17:04:38
182,675
2025-03-11 07:14:36
https://stackoverflow.com/q/3349105
https://stackoverflow.com/a/3355423
<h2>TL;DR</h2> <pre class="lang-bash prettyprint-override"><code>#!/usr/bin/env bash cd &quot;$(dirname &quot;$0&quot;)&quot; </code></pre> <h2>The explanation</h2> <p>How does this work and how does it deal with edge and corner cases?</p> <ul> <li>You type a script invocation command into your interactive shell, which...
<h2>TL;DR</h2> <pre class="lang-bash prettyprint-override"><code>#!/usr/bin/env bash cd &quot;$(dirname &quot;$0&quot;)&quot; </code></pre> <h2>The explanation</h2> <p>How does this work and how does it deal with edge and corner cases?</p> <ul> <li>You type a script invocation command into your interactive shell, which...
387, 531, 6268
bash, path, scripting
<h1>How can I set the current working directory to the directory of the script in Bash?</h1> <p>I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in.</p> <p>The default behavior is that the current working directory in the script is that of the shell f...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
144
bash
# How can I set the current working directory to the directory of the script in Bash? I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in. The default behavior is that the current working directory in the script is that of the shell from which I run...
## TL;DR ``` #!/usr/bin/env bash cd "$(dirname "$0")" ``` ## The explanation How does this work and how does it deal with edge and corner cases? - You type a script invocation command into your interactive shell, which may or may not be bash. - That interactive shell tells the kernel to execute the script from the ...
3327013
How to determine the current interactive shell that I'm in (command-line)
864
2010-07-24 21:35:28
<p>How can I determine the current shell I am working on? </p> <p>Would the output of the <code>ps</code> command alone be sufficient?</p> <p>How can this be done in different flavors of Unix?</p>
721,729
268,543
2025-03-13 15:38:33
3,327,022
1,024
2010-07-24 21:37:34
119,280
2025-03-13 15:38:33
https://stackoverflow.com/q/3327013
https://stackoverflow.com/a/3327022
<p>There are three approaches to finding the <strong>name</strong> of the current shell's executable:</p> <p>Please note that all three approaches can be fooled if the executable of the shell is <code>/bin/sh</code>, but it's really a renamed <code>bash</code>, for example (which frequently happens).</p> <p><em>Thus yo...
<p>There are three approaches to finding the <strong>name</strong> of the current shell's executable:</p> <p>Please note that all three approaches can be fooled if the executable of the shell is <code>/bin/sh</code>, but it's really a renamed <code>bash</code>, for example (which frequently happens).</p> <p><em>Thus yo...
34, 387, 390, 2829, 8599
bash, csh, shell, tcsh, unix
<h1>How to determine the current interactive shell that I'm in (command-line)</h1> <p>How can I determine the current shell I am working on? </p> <p>Would the output of the <code>ps</code> command alone be sufficient?</p> <p>How can this be done in different flavors of Unix?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
145
bash
# How to determine the current interactive shell that I'm in (command-line) How can I determine the current shell I am working on? Would the output of the `ps` command alone be sufficient? How can this be done in different flavors of Unix?
There are three approaches to finding the **name** of the current shell's executable: Please note that all three approaches can be fooled if the executable of the shell is `/bin/sh`, but it's really a renamed `bash`, for example (which frequently happens). *Thus your second question of whether `ps` output will do is ...
821396
Aborting a shell script if any command returns a non-zero value
628
2009-05-04 18:55:50
<p>I have a Bash shell script that invokes a number of commands.</p> <p>I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value.</p> <p>Is this possible without explicitly checking the result of each command?</p> <p>For example,</p> <pre><code>dos...
276,104
93,430
2024-10-28 09:37:39
821,419
1,010
2009-05-04 19:00:16
7,446
2022-09-14 21:23:55
https://stackoverflow.com/q/821396
https://stackoverflow.com/a/821419
<p>Add this to the beginning of the script:</p> <pre><code>set -e </code></pre> <p>This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an <code>if</code>, <code>while</code>, or <code>until</code> test, or part of an <code>&amp;...
<p>Add this to the beginning of the script:</p> <pre><code>set -e </code></pre> <p>This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an <code>if</code>, <code>while</code>, or <code>until</code> test, or part of an <code>&amp;...
34, 58, 387, 390
bash, linux, shell, unix
<h1>Aborting a shell script if any command returns a non-zero value</h1> <p>I have a Bash shell script that invokes a number of commands.</p> <p>I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value.</p> <p>Is this possible without explicitly ch...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
146
bash
# Aborting a shell script if any command returns a non-zero value I have a Bash shell script that invokes a number of commands. I would like to have the shell script automatically exit with a return value of 1 if any of the commands return a non-zero value. Is this possible without explicitly checking the result of ...
Add this to the beginning of the script: ``` set -e ``` This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an `if`, `while`, or `until` test, or part of an `&&` or `||` list. See the [bash manual](https://www.gnu.org/software...
9057387
Process all arguments except the first one (in a bash script)
685
2012-01-29 22:31:22
<p>I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script.</p> <p>Using Google I found <a href="http://wiki.bash-hackers.org/scripting/posparams#mass_usage" rel="noreferrer">this wiki</a>, but it provided a literal exa...
317,321
992,005
2024-04-23 15:29:11
9,057,392
991
2012-01-29 22:32:55
129,570
2016-04-01 02:29:12
https://stackoverflow.com/q/9057387
https://stackoverflow.com/a/9057392
<p>Use this:</p> <pre><code>echo "${@:2}" </code></pre> <hr> <p>The following syntax:</p> <pre><code>echo "${*:2}" </code></pre> <p>would work as well, but is not recommended, because as <a href="https://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one#comment11369452_9057392">@Gordon...
<p>Use this:</p> <pre><code>echo "${@:2}" </code></pre> <hr> <p>The following syntax:</p> <pre><code>echo "${*:2}" </code></pre> <p>would work as well, but is not recommended, because as <a href="https://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one#comment11369452_9057392">@Gordon...
387, 390
bash, shell
<h1>Process all arguments except the first one (in a bash script)</h1> <p>I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script.</p> <p>Using Google I found <a href="http://wiki.bash-hackers.org/scripting/posparams#ma...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
147
bash
# Process all arguments except the first one (in a bash script) I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script. Using Google I found [this wiki](http://wiki.bash-hackers.org/scripting/posparams#mass_usage), b...
Use this: ``` echo "${@:2}" ``` --- The following syntax: ``` echo "${*:2}" ``` would work as well, but is not recommended, because as [@Gordon](https://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one#comment11369452_9057392) already explained, that using `*`, it runs all of the argu...
83329
How can I extract a predetermined range of lines from a text file on Unix?
694
2008-09-17 13:40:59
<p>I have a <code>~23000</code> line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that I want.</p> <p>Does anyone know a Unix command (or seri...
692,777
15,676
2024-08-02 22:39:30
83,347
990
2008-09-17 13:42:34
15,732
2018-06-13 18:09:58
https://stackoverflow.com/q/83329
https://stackoverflow.com/a/83347
<pre><code>sed -n '16224,16482p;16483q' filename &gt; newfile </code></pre> <p>From the <a href="https://www.gnu.org/software/sed/manual/sed.html#Common-Commands" rel="noreferrer">sed manual</a>:</p> <blockquote> <p><strong>p</strong> - Print out the pattern space (to the standard output). This command is us...
<pre><code>sed -n '16224,16482p;16483q' filename &gt; newfile </code></pre> <p>From the <a href="https://www.gnu.org/software/sed/manual/sed.html#Common-Commands" rel="noreferrer">sed manual</a>:</p> <blockquote> <p><strong>p</strong> - Print out the pattern space (to the standard output). This command is us...
387, 1718
bash, text-processing
<h1>How can I extract a predetermined range of lines from a text file on Unix?</h1> <p>I have a <code>~23000</code> line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end l...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
148
bash
# How can I extract a predetermined range of lines from a text file on Unix? I have a `~23000` line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the d...
``` sed -n '16224,16482p;16483q' filename > newfile ``` From the [sed manual](https://www.gnu.org/software/sed/manual/sed.html#Common-Commands): > **p** - > Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. > > **n** - > If auto-pri...
5130968
How can I copy the output of a command directly into my clipboard?
927
2011-02-27 02:10:46
<p>How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:</p> <pre><code>cat file | clipboard </code></pre>
886,690
184,046
2025-05-27 14:38:10
5,130,969
989
2011-02-27 02:11:09
184,046
2023-05-17 09:17:49
https://stackoverflow.com/q/5130968
https://stackoverflow.com/a/5130969
<p>One way of doing it follows:</p> <ol> <li><p>Install <code>xclip</code>, such as:</p> <p><code>sudo apt-get install xclip</code></p> </li> <li><p>Pipe the output into <code>xclip</code> to be copied into the clipboard:</p> <p><code>cat file | xclip</code></p> </li> <li><p>Paste the text you just copied into a <code>...
<p>One way of doing it follows:</p> <ol> <li><p>Install <code>xclip</code>, such as:</p> <p><code>sudo apt-get install xclip</code></p> </li> <li><p>Pipe the output into <code>xclip</code> to be copied into the clipboard:</p> <p><code>cat file | xclip</code></p> </li> <li><p>Paste the text you just copied into a <code>...
34, 58, 390, 391, 524
clipboard, linux, shell, terminal, unix
<h1>How can I copy the output of a command directly into my clipboard?</h1> <p>How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance:</p> <pre><code>cat file | clipboard </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
149
bash
# How can I copy the output of a command directly into my clipboard? How can I pipe the output of a command into my clipboard and paste it back when using a terminal? For instance: ``` cat file | clipboard ```
One way of doing it follows: 1. Install `xclip`, such as: `sudo apt-get install xclip` 2. Pipe the output into `xclip` to be copied into the clipboard: `cat file | xclip` 3. Paste the text you just copied into a `X` application: `xclip -o` To paste somewhere else other than an `X` application, such as a t...
22009364
Is there a TRY CATCH command in Bash
671
2014-02-25 09:09:14
<p>I'm writing a shell script and need to check that a terminal app has been installed. I want to use a TRY/CATCH command to do this unless there is a neater way.</p>
731,713
107,567
2023-11-21 05:03:10
22,010,339
989
2014-02-25 09:50:02
3,150,853
2020-02-19 05:20:51
https://stackoverflow.com/q/22009364
https://stackoverflow.com/a/22010339
<blockquote> <p>Is there a TRY CATCH command in Bash?</p> </blockquote> <p>No. </p> <p>Bash doesn't have as many luxuries as one can find in many programming languages.</p> <p>There is no <code>try/catch</code> in bash; however, one can achieve similar behavior using <code>&amp;&amp;</code> or <code>||</code>.</p...
<blockquote> <p>Is there a TRY CATCH command in Bash?</p> </blockquote> <p>No. </p> <p>Bash doesn't have as many luxuries as one can find in many programming languages.</p> <p>There is no <code>try/catch</code> in bash; however, one can achieve similar behavior using <code>&amp;&amp;</code> or <code>||</code>.</p...
379, 387, 390
bash, error-handling, shell
<h1>Is there a TRY CATCH command in Bash</h1> <p>I'm writing a shell script and need to check that a terminal app has been installed. I want to use a TRY/CATCH command to do this unless there is a neater way.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
150
bash
# Is there a TRY CATCH command in Bash I'm writing a shell script and need to check that a terminal app has been installed. I want to use a TRY/CATCH command to do this unless there is a neater way.
> Is there a TRY CATCH command in Bash? No. Bash doesn't have as many luxuries as one can find in many programming languages. There is no `try/catch` in bash; however, one can achieve similar behavior using `&&` or `||`. Using `||`: if `command1` fails then `command2` runs as follows ``` command1 || command2 ``` ...
5064563
How to add line break to 'git commit -m' from the command line?
1,119
2011-02-21 10:02:42
<p>I am using Git from the command line and am trying to add a line break to the commit message (using <code>git commit -m ""</code>) without going into Vim.</p> <p>Is this possible?</p>
505,211
347,180
2024-08-16 08:44:42
5,064,653
985
2011-02-21 10:12:01
613,064
2019-08-22 11:35:15
https://stackoverflow.com/q/5064563
https://stackoverflow.com/a/5064653
<p>Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this:</p> <pre><code>git commit -m 'Message goes here' </code></pre> <p>Alternatively, you can use ...
<p>Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this:</p> <pre><code>git commit -m 'Message goes here' </code></pre> <p>Alternatively, you can use ...
119, 387, 390
bash, git, shell
<h1>How to add line break to 'git commit -m' from the command line?</h1> <p>I am using Git from the command line and am trying to add a line break to the commit message (using <code>git commit -m ""</code>) without going into Vim.</p> <p>Is this possible?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
151
bash
# How to add line break to 'git commit -m' from the command line? I am using Git from the command line and am trying to add a line break to the commit message (using `git commit -m ""`) without going into Vim. Is this possible?
Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this: ``` git commit -m 'Message goes here' ``` Alternatively, you can use a "here document" (also kno...
3235850
How to enter a multi-line command
711
2010-07-13 09:36:16
<p>Is it possible to split a PowerShell command line over multiple lines?</p> <p>In Visual Basic I can use the underscore (<code>_</code>) to continue the command in the next line.</p>
718,654
83,004
2025-02-13 21:25:50
3,235,993
985
2010-07-13 09:52:44
73,070
2018-12-13 13:34:50
https://stackoverflow.com/q/3235850
https://stackoverflow.com/a/3235993
<p>You can use a space followed by the grave accent (backtick):</p> <pre><code>Get-ChildItem -Recurse ` -Filter *.jpg ` | Select LastWriteTime </code></pre> <p>However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be comp...
<p>You can use a space followed by the grave accent (backtick):</p> <pre><code>Get-ChildItem -Recurse ` -Filter *.jpg ` | Select LastWriteTime </code></pre> <p>However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be comp...
526, 10941
multiline, powershell
<h1>How to enter a multi-line command</h1> <p>Is it possible to split a PowerShell command line over multiple lines?</p> <p>In Visual Basic I can use the underscore (<code>_</code>) to continue the command in the next line.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
152
bash
# How to enter a multi-line command Is it possible to split a PowerShell command line over multiple lines? In Visual Basic I can use the underscore (`_`) to continue the command in the next line.
You can use a space followed by the grave accent (backtick): ``` Get-ChildItem -Recurse ` -Filter *.jpg ` | Select LastWriteTime ``` However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be complete at that point. This in...
5171901
Find and replace in file and overwrite file doesn't work, it empties the file
638
2011-03-02 18:44:28
<p>I would like to run a find and replace on an HTML file through the command line.</p> <p>My command looks something like this:</p> <pre><code>sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html &gt; index.html </code></pre> <p>When I run this and look at the file afterward, it is empty. It deleted the con...
758,956
540,738
2019-04-04 19:18:53
5,171,935
985
2011-03-02 18:48:27
227,665
2012-07-23 07:02:27
https://stackoverflow.com/q/5171901
https://stackoverflow.com/a/5171935
<p>When the <strong>shell</strong> sees <code>&gt; index.html</code> in the command line it opens the file <code>index.html</code> for <strong>writing</strong>, wiping off all its previous contents.</p> <p>To fix this you need to pass the <code>-i</code> option to <code>sed</code> to make the changes inline and creat...
<p>When the <strong>shell</strong> sees <code>&gt; index.html</code> in the command line it opens the file <code>index.html</code> for <strong>writing</strong>, wiping off all its previous contents.</p> <p>To fix this you need to pass the <code>-i</code> option to <code>sed</code> to make the changes inline and creat...
34, 390, 5282, 26698
io-redirection, sed, shell, unix
<h1>Find and replace in file and overwrite file doesn't work, it empties the file</h1> <p>I would like to run a find and replace on an HTML file through the command line.</p> <p>My command looks something like this:</p> <pre><code>sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html &gt; index.html </code></p...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
153
bash
# Find and replace in file and overwrite file doesn't work, it empties the file I would like to run a find and replace on an HTML file through the command line. My command looks something like this: ``` sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html > index.html ``` When I run this and look at the fil...
When the **shell** sees `> index.html` in the command line it opens the file `index.html` for **writing**, wiping off all its previous contents. To fix this you need to pass the `-i` option to `sed` to make the changes inline and create a backup of the original file before it does the changes in-place: ``` sed -i.bak...
16661982
Check folder size in Bash
516
2013-05-21 04:08:36
<p>I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?</p> <pre><code># 10GB SIZE="1074747474" # check the current size CHECK="`du /data/sflow_log/`" if [ "$CHECK" -gt "$SIZE" ]; then ec...
534,356
2,403,913
2023-02-19 17:40:02
16,662,027
981
2013-05-21 04:12:55
1,246,262
2021-06-17 06:53:14
https://stackoverflow.com/q/16661982
https://stackoverflow.com/a/16662027
<p>You can do:</p> <pre><code>du -hs your_directory </code></pre> <p>which will give you a brief output of the size of your target directory. Using a wildcard like <code>*</code> can select multiple directories.</p> <p>If you want a full listing of sizes for all files and sub-directories inside your target, you can do:...
<p>You can do:</p> <pre><code>du -hs your_directory </code></pre> <p>which will give you a brief output of the size of your target directory. Using a wildcard like <code>*</code> can select multiple directories.</p> <p>If you want a full listing of sizes for all files and sub-directories inside your target, you can do:...
218, 387, 390
bash, directory, shell
<h1>Check folder size in Bash</h1> <p>I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name?</p> <pre><code># 10GB SIZE="1074747474" # check the current size CHECK="`du /data/sflow_log/`" if ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
154
bash
# Check folder size in Bash I'm trying to write a script that will calculate a directory size and if the size is less than 10GB, and greater then 2GB do some action. Where do I need to mention my folder name? ``` # 10GB SIZE="1074747474" # check the current size CHECK="`du /data/sflow_log/`" if [ "$CHECK" -gt "$SIZE...
You can do: ``` du -hs your_directory ``` which will give you a brief output of the size of your target directory. Using a wildcard like `*` can select multiple directories. If you want a full listing of sizes for all files and sub-directories inside your target, you can do: ``` du -h your_directory ``` **Tips:** ...
221921
Use grep --exclude/--include syntax to not grep through certain files
951
2008-10-21 13:41:01
<p>I'm looking for the string <code>foo=</code> in text files in a directory tree. It's on a common Linux machine, I have bash shell:</p> <pre><code>grep -ircl &quot;foo=&quot; * </code></pre> <p>In the directories are also many binary files which match <code>&quot;foo=&quot;</code>. As these results are not relevant a...
913,010
19,746
2023-07-28 11:13:22
221,929
979
2008-10-21 13:44:01
9,530
2021-03-04 19:27:56
https://stackoverflow.com/q/221921
https://stackoverflow.com/a/221929
<p>Use the shell <a href="https://man7.org/linux/man-pages/man7/glob.7.html" rel="noreferrer">globbing syntax</a>:</p> <pre><code>grep pattern -r --include=\*.cpp --include=\*.h rootdir </code></pre> <p>The syntax for <code>--exclude</code> is identical.</p> <p>Note that the star is escaped with a backslash to prevent ...
<p>Use the shell <a href="https://man7.org/linux/man-pages/man7/glob.7.html" rel="noreferrer">globbing syntax</a>:</p> <pre><code>grep pattern -r --include=\*.cpp --include=\*.h rootdir </code></pre> <p>The syntax for <code>--exclude</code> is identical.</p> <p>Note that the star is escaped with a backslash to prevent ...
34, 390, 816, 1231, 1271
command-line, grep, search, shell, unix
<h1>Use grep --exclude/--include syntax to not grep through certain files</h1> <p>I'm looking for the string <code>foo=</code> in text files in a directory tree. It's on a common Linux machine, I have bash shell:</p> <pre><code>grep -ircl &quot;foo=&quot; * </code></pre> <p>In the directories are also many binary files...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
155
bash
# Use grep --exclude/--include syntax to not grep through certain files I'm looking for the string `foo=` in text files in a directory tree. It's on a common Linux machine, I have bash shell: ``` grep -ircl "foo=" * ``` In the directories are also many binary files which match `"foo="`. As these results are not rele...
Use the shell [globbing syntax](https://man7.org/linux/man-pages/man7/glob.7.html): ``` grep pattern -r --include=\*.cpp --include=\*.h rootdir ``` The syntax for `--exclude` is identical. Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as `--include="*...
6121091
How to extract directory path from file path?
619
2011-05-25 07:41:44
<p>In Bash, if <code>VAR="/home/me/mydir/file.c"</code>, how do I get <code>"/home/me/mydir"</code>?</p>
672,453
579,689
2024-03-19 14:33:33
6,121,114
974
2011-05-25 07:44:16
14,860
2021-08-28 23:57:15
https://stackoverflow.com/q/6121091
https://stackoverflow.com/a/6121114
<p><code>dirname</code> and <code>basename</code> are the tools you're looking for for extracting path components:</p> <pre><code>$ VAR='/home/pax/file.c' $ DIR=&quot;$(dirname &quot;${VAR}&quot;)&quot; ; FILE=&quot;$(basename &quot;${VAR}&quot;)&quot; $ echo &quot;[${DIR}] [${FILE}]&quot; [/home/pax] [file.c] </code><...
<p><code>dirname</code> and <code>basename</code> are the tools you're looking for for extracting path components:</p> <pre><code>$ VAR='/home/pax/file.c' $ DIR=&quot;$(dirname &quot;${VAR}&quot;)&quot; ; FILE=&quot;$(basename &quot;${VAR}&quot;)&quot; $ echo &quot;[${DIR}] [${FILE}]&quot; [/home/pax] [file.c] </code><...
387
bash
<h1>How to extract directory path from file path?</h1> <p>In Bash, if <code>VAR="/home/me/mydir/file.c"</code>, how do I get <code>"/home/me/mydir"</code>?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
156
bash
# How to extract directory path from file path? In Bash, if `VAR="/home/me/mydir/file.c"`, how do I get `"/home/me/mydir"`?
`dirname` and `basename` are the tools you're looking for for extracting path components: ``` $ VAR='/home/pax/file.c' $ DIR="$(dirname "${VAR}")" ; FILE="$(basename "${VAR}")" $ echo "[${DIR}] [${FILE}]" [/home/pax] [file.c] ``` They're not internal `bash` commands but they are part of the POSIX standard - see [`dir...
6697753
Difference between single and double quotes in Bash
963
2011-07-14 17:55:27
<p>In Bash, what are the differences between single quotes (<code>''</code>) and double quotes (<code>""</code>)?</p>
475,900
318,760
2025-07-24 15:22:29
6,697,781
969
2011-07-14 17:57:44
120,808
2025-07-23 13:56:29
https://stackoverflow.com/q/6697753
https://stackoverflow.com/a/6697781
<p>Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain <code>\</code> escapes, etc.</p> <p>Example:</p> <pre class="lang-bash prettyprint-override"><code>echo &quot;$(echo &quot;upg&quot;)&quot; </code></pre> <p>Output:</p> <pre class="lang-bash prettyprint-overr...
<p>Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain <code>\</code> escapes, etc.</p> <p>Example:</p> <pre class="lang-bash prettyprint-override"><code>echo &quot;$(echo &quot;upg&quot;)&quot; </code></pre> <p>Output:</p> <pre class="lang-bash prettyprint-overr...
387, 390, 430, 63342, 108300
bash, double-quotes, quotes, shell, single-quotes
<h1>Difference between single and double quotes in Bash</h1> <p>In Bash, what are the differences between single quotes (<code>''</code>) and double quotes (<code>""</code>)?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
157
bash
# Difference between single and double quotes in Bash In Bash, what are the differences between single quotes (`''`) and double quotes (`""`)?
Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain `\` escapes, etc. Example: ``` echo "$(echo "upg")" ``` Output: ``` upg ``` And: ``` echo '$(echo "upg")' ``` Output: ``` $(echo "upg") ``` The Bash manual has this to say: > [3.1.2.2 Single Quotes](ht...
9146123
Pretty print in MongoDB shell as default
577
2012-02-05 01:19:52
<p>Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.</p>
171,965
791,406
2019-05-13 06:51:29
9,146,646
958
2012-02-05 03:19:23
125,816
2013-10-28 10:46:52
https://stackoverflow.com/q/9146123
https://stackoverflow.com/a/9146646
<p>(<em>note: this is answer to original version of the question, which did not have requirements for "default"</em>)</p> <p>You can ask it to be pretty.</p> <pre><code>db.collection.find().pretty() </code></pre>
<p>(<em>note: this is answer to original version of the question, which did not have requirements for "default"</em>)</p> <p>You can ask it to be pretty.</p> <pre><code>db.collection.find().pretty() </code></pre>
11225, 30073, 69521
command-line-interface, mongodb, mongo-shell
<h1>Pretty print in MongoDB shell as default</h1> <p>Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
158
bash
# Pretty print in MongoDB shell as default Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.
(*note: this is answer to original version of the question, which did not have requirements for "default"*) You can ask it to be pretty. ``` db.collection.find().pretty() ```
428109
Extract substring in Bash
1,197
2009-01-09 13:53:22
<p>Given a filename in the form <code>someletters_12345_moreleters.ext</code>, I want to extract the 5 digits and put them into a variable.</p> <p>So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side then another set of x n...
2,076,499
39,666
2025-04-29 08:09:40
428,118
947
2009-01-09 13:56:14
40,441
2015-11-04 22:51:55
https://stackoverflow.com/q/428109
https://stackoverflow.com/a/428118
<p>Use <a href="http://www.manpagez.com/man/1/cut/" rel="noreferrer">cut</a>:</p> <pre><code>echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2 </code></pre> <p>More generic:</p> <pre><code>INPUT='someletters_12345_moreleters.ext' SUBSTRING=$(echo $INPUT| cut -d'_' -f 2) echo $SUBSTRING </code></pre>
<p>Use <a href="http://www.manpagez.com/man/1/cut/" rel="noreferrer">cut</a>:</p> <pre><code>echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2 </code></pre> <p>More generic:</p> <pre><code>INPUT='someletters_12345_moreleters.ext' SUBSTRING=$(echo $INPUT| cut -d'_' -f 2) echo $SUBSTRING </code></pre>
139, 387, 390, 4371
bash, shell, string, substring
<h1>Extract substring in Bash</h1> <p>Given a filename in the form <code>someletters_12345_moreleters.ext</code>, I want to extract the 5 digits and put them into a variable.</p> <p>So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
159
bash
# Extract substring in Bash Given a filename in the form `someletters_12345_moreleters.ext`, I want to extract the 5 digits and put them into a variable. So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side then another se...
Use [cut](http://www.manpagez.com/man/1/cut/): ``` echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2 ``` More generic: ``` INPUT='someletters_12345_moreleters.ext' SUBSTRING=$(echo $INPUT| cut -d'_' -f 2) echo $SUBSTRING ```
2764051
How to join multiple lines of filenames into one with custom delimiter
592
2010-05-04 09:09:40
<p>How do I join the result of <code>ls -1</code> into a single line and <a href="https://en.wikipedia.org/wiki/Delimiter" rel="noreferrer">delimit</a> it with whatever I want?</p>
331,384
99,033
2024-11-12 00:58:32
6,539,865
944
2011-06-30 19:10:36
823,691
2023-02-19 20:44:14
https://stackoverflow.com/q/2764051
https://stackoverflow.com/a/6539865
<p><code>paste -s -d</code> joins lines with a delimiter (e.g. <code>&quot;,&quot;</code>), and does not leave a trailing delimiter:</p> <pre><code>ls -1 | paste -sd &quot;,&quot; - </code></pre>
<p><code>paste -s -d</code> joins lines with a delimiter (e.g. <code>&quot;,&quot;</code>), and does not leave a trailing delimiter:</p> <pre><code>ls -1 | paste -sd &quot;,&quot; - </code></pre>
58, 139, 387, 390, 1357
bash, linux, parsing, shell, string
<h1>How to join multiple lines of filenames into one with custom delimiter</h1> <p>How do I join the result of <code>ls -1</code> into a single line and <a href="https://en.wikipedia.org/wiki/Delimiter" rel="noreferrer">delimit</a> it with whatever I want?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
160
bash
# How to join multiple lines of filenames into one with custom delimiter How do I join the result of `ls -1` into a single line and [delimit](https://en.wikipedia.org/wiki/Delimiter) it with whatever I want?
`paste -s -d` joins lines with a delimiter (e.g. `","`), and does not leave a trailing delimiter: ``` ls -1 | paste -sd "," - ```
4572153
OS X: equivalent of Linux's wget
673
2010-12-31 20:19:54
<p>How can I do an HTTP GET from a Un*x shell script on a stock OS X system? (installing third-party software is not an option, for this has to run on a lot of different systems which I don't have control on).</p> <p>For example if I start the Mercurial server locally doing a <em>hg serve</em>:</p> <pre><code>... $ h...
818,877
257,356
2022-10-29 16:41:32
4,572,158
943
2010-12-31 20:21:26
207,248
2022-10-29 16:41:32
https://stackoverflow.com/q/4572153
https://stackoverflow.com/a/4572158
<p>The following native command will work:</p> <p><code>curl http://127.0.0.1:8000 -o outfile</code></p> <p>Note that <code>curl</code> does not follow redirects by default. To tell it to do so, add <code>-L</code> to the argument list.</p>
<p>The following native command will work:</p> <p><code>curl http://127.0.0.1:8000 -o outfile</code></p> <p>Note that <code>curl</code> does not follow redirects by default. To tell it to do so, add <code>-L</code> to the argument list.</p>
34, 369, 390, 6810
http-get, macos, shell, unix
<h1>OS X: equivalent of Linux's wget</h1> <p>How can I do an HTTP GET from a Un*x shell script on a stock OS X system? (installing third-party software is not an option, for this has to run on a lot of different systems which I don't have control on).</p> <p>For example if I start the Mercurial server locally doing a ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
161
bash
# OS X: equivalent of Linux's wget How can I do an HTTP GET from a Un*x shell script on a stock OS X system? (installing third-party software is not an option, for this has to run on a lot of different systems which I don't have control on). For example if I start the Mercurial server locally doing a *hg serve*: ```...
The following native command will work: `curl http://127.0.0.1:8000 -o outfile` Note that `curl` does not follow redirects by default. To tell it to do so, add `-L` to the argument list.
394230
How to detect the OS from a Bash script?
841
2008-12-26 20:27:35
<p>I would like to keep my <code>.bashrc</code> and <code>.bash_login</code> files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running on Mac&nbsp;OS&nbsp;X, Linux or <a href="http://en...
534,665
19,839
2025-09-18 02:27:09
8,597,411
938
2011-12-21 22:46:47
265,521
2020-05-05 12:08:57
https://stackoverflow.com/q/394230
https://stackoverflow.com/a/8597411
<p>I think the following should work. I'm not sure about <code>win32</code> though.</p> <pre><code>if [[ "$OSTYPE" == "linux-gnu"* ]]; then # ... elif [[ "$OSTYPE" == "darwin"* ]]; then # Mac OSX elif [[ "$OSTYPE" == "cygwin" ]]; then # POSIX compatibility layer and Linux environment emulation ...
<p>I think the following should work. I'm not sure about <code>win32</code> though.</p> <pre><code>if [[ "$OSTYPE" == "linux-gnu"* ]]; then # ... elif [[ "$OSTYPE" == "darwin"* ]]; then # Mac OSX elif [[ "$OSTYPE" == "cygwin" ]]; then # POSIX compatibility layer and Linux environment emulation ...
387, 97338
bash, os-detection
<h1>How to detect the OS from a Bash script?</h1> <p>I would like to keep my <code>.bashrc</code> and <code>.bash_login</code> files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
162
bash
# How to detect the OS from a Bash script? I would like to keep my `.bashrc` and `.bash_login` files in version control so that I can use them between all the computers I use. The problem is I have some OS specific aliases so I was looking for a way to determine if the script is running on Mac OS X, Linux or [Cygwin](...
I think the following should work. I'm not sure about `win32` though. ``` if [[ "$OSTYPE" == "linux-gnu"* ]]; then # ... elif [[ "$OSTYPE" == "darwin"* ]]; then # Mac OSX elif [[ "$OSTYPE" == "cygwin" ]]; then # POSIX compatibility layer and Linux environment emulation for Windows elif [[ "$OST...
320509
Is it possible to open a Windows Explorer window from PowerShell?
449
2008-11-26 12:19:08
<p>I'm sure this must be possible, but I can't find out how to do it.</p> <p>Any clues?</p>
219,957
1,088,682
2024-09-26 08:00:43
321,092
932
2008-11-26 15:25:25
1,358
2023-09-12 13:14:33
https://stackoverflow.com/q/320509
https://stackoverflow.com/a/321092
<p>Use:</p> <pre><code>ii . </code></pre> <p>which is short for</p> <pre><code>Invoke-Item . </code></pre> <p>The dot can be substituted with any path.</p>
<p>Use:</p> <pre><code>ii . </code></pre> <p>which is short for</p> <pre><code>Invoke-Item . </code></pre> <p>The dot can be substituted with any path.</p>
526, 638
explorer, powershell
<h1>Is it possible to open a Windows Explorer window from PowerShell?</h1> <p>I'm sure this must be possible, but I can't find out how to do it.</p> <p>Any clues?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
163
bash
# Is it possible to open a Windows Explorer window from PowerShell? I'm sure this must be possible, but I can't find out how to do it. Any clues?
Use: ``` ii . ``` which is short for ``` Invoke-Item . ``` The dot can be substituted with any path.
3980668
How to get a password from a shell script without echoing
647
2010-10-20 17:33:28
<p>I have a script that automates a process that needs access to a password-protected system. The system is accessed via a command-line program that accepts the user password as an argument.</p> <p>I would like to prompt the user to type in their password, assign it to a shell variable, and then use that variable to co...
685,404
220,826
2023-12-05 00:25:11
3,980,904
930
2010-10-20 18:04:18
480,767
2023-04-02 18:40:39
https://stackoverflow.com/q/3980668
https://stackoverflow.com/a/3980904
<p>Here is another way to do it:</p> <pre><code>#!/bin/bash # Read Password echo -n Password: read -s password echo # Run Command echo $password </code></pre> <p>The <code>read -s</code> will turn off echo for you. Just replace the <code>echo</code> on the last line with the command you want to run.</p> <p>In some she...
<p>Here is another way to do it:</p> <pre><code>#!/bin/bash # Read Password echo -n Password: read -s password echo # Run Command echo $password </code></pre> <p>The <code>read -s</code> will turn off echo for you. Just replace the <code>echo</code> on the last line with the command you want to run.</p> <p>In some she...
387, 390, 531, 10327
bash, scripting, sh, shell
<h1>How to get a password from a shell script without echoing</h1> <p>I have a script that automates a process that needs access to a password-protected system. The system is accessed via a command-line program that accepts the user password as an argument.</p> <p>I would like to prompt the user to type in their passwo...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
164
bash
# How to get a password from a shell script without echoing I have a script that automates a process that needs access to a password-protected system. The system is accessed via a command-line program that accepts the user password as an argument. I would like to prompt the user to type in their password, assign it t...
Here is another way to do it: ``` #!/bin/bash # Read Password echo -n Password: read -s password echo # Run Command echo $password ``` The `read -s` will turn off echo for you. Just replace the `echo` on the last line with the command you want to run. In some shells (e.g. Bash) [`read`](https://linuxcommand.org/lc3...
58032631
Why powershell does not run Angular commands?
367
2019-09-20 17:11:21
<p>I have started to learn Angular but I note that powershell in Windows gives me an error whenever I make an angular command like:</p> <pre><code>ng new new-app </code></pre> <p>or</p> <pre><code>ng serve </code></pre> <p>this is the error what I got:</p> <pre><code>ng : File C:\Users\&lt; username &gt;\AppData\R...
513,662
11,307,065
2024-06-21 06:54:51
58,044,573
913
2019-09-21 22:23:06
11,307,065
2020-02-11 23:31:49
https://stackoverflow.com/q/58032631
https://stackoverflow.com/a/58044573
<p>Remove <code>ng.ps1</code> from the directory <code>C:\Users\%username%\AppData\Roaming\npm\</code> then try clearing the npm cache at <code>C:\Users\%username%\AppData\Roaming\npm-cache\</code> </p>
<p>Remove <code>ng.ps1</code> from the directory <code>C:\Users\%username%\AppData\Roaming\npm\</code> then try clearing the npm cache at <code>C:\Users\%username%\AppData\Roaming\npm-cache\</code> </p>
3, 526, 117722, 125866
angular, angular-cli, javascript, powershell
<h1>Why powershell does not run Angular commands?</h1> <p>I have started to learn Angular but I note that powershell in Windows gives me an error whenever I make an angular command like:</p> <pre><code>ng new new-app </code></pre> <p>or</p> <pre><code>ng serve </code></pre> <p>this is the error what I got:</p> <pr...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
165
bash
# Why powershell does not run Angular commands? I have started to learn Angular but I note that powershell in Windows gives me an error whenever I make an angular command like: ``` ng new new-app ``` or ``` ng serve ``` this is the error what I got: ``` ng : File C:\Users\< username >\AppData\Roaming\npm\ng.ps1 c...
Remove `ng.ps1` from the directory `C:\Users\%username%\AppData\Roaming\npm\` then try clearing the npm cache at `C:\Users\%username%\AppData\Roaming\npm-cache\`
39296472
How to check if an environment variable exists and get its value?
566
2016-09-02 15:55:13
<p>I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present.</p> <p>For instance, assume, before running the script, I perform the following operation:</p> <pre><code...
909,254
2,865,564
2024-09-08 14:26:38
39,296,583
909
2016-09-02 16:02:25
6,658,477
2018-05-14 17:26:05
https://stackoverflow.com/q/39296472
https://stackoverflow.com/a/39296583
<p><code>[ -z "${DEPLOY_ENV}" ]</code> checks whether <code>DEPLOY_ENV</code> has length equal to zero. So you could run:</p> <pre><code>if [[ -z "${DEPLOY_ENV}" ]]; then MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined" else MY_SCRIPT_VARIABLE="${DEPLOY_ENV}" fi # or using a short-hand vers...
<p><code>[ -z "${DEPLOY_ENV}" ]</code> checks whether <code>DEPLOY_ENV</code> has length equal to zero. So you could run:</p> <pre><code>if [[ -z "${DEPLOY_ENV}" ]]; then MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined" else MY_SCRIPT_VARIABLE="${DEPLOY_ENV}" fi # or using a short-hand vers...
387, 390
bash, shell
<h1>How to check if an environment variable exists and get its value?</h1> <p>I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present.</p> <p>For instance, assume, be...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
166
bash
# How to check if an environment variable exists and get its value? I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present. For instance, assume, before running the...
`[ -z "${DEPLOY_ENV}" ]` checks whether `DEPLOY_ENV` has length equal to zero. So you could run: ``` if [[ -z "${DEPLOY_ENV}" ]]; then MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined" else MY_SCRIPT_VARIABLE="${DEPLOY_ENV}" fi # or using a short-hand version [[ -z "${DEPLOY_ENV}" ]] && MyV...
44435697
Change the default terminal in Visual Studio Code
518
2017-06-08 12:23:15
<p>I am using Visual Studio Code on my Windows 10 PC. I want to change my default terminal from Windows <a href="https://en.wikipedia.org/wiki/PowerShell" rel="noreferrer">PowerShell</a> to <a href="https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> on <a href="https://en.wikipedia.org/wiki/...
674,018
6,735,912
2025-06-04 08:42:11
45,899,693
908
2017-08-26 21:10:09
2,535,344
2023-08-11 21:42:19
https://stackoverflow.com/q/44435697
https://stackoverflow.com/a/45899693
<p>You can also select your default terminal by pressing <kbd>F1</kbd> in Visual Studio Code and typing/selecting <strong><code>Terminal: Select Default Profile</code></strong> (or <strong><code>Terminal: Select Default Shell</code></strong> in older Visual Studio Code versions).</p> <p><img src="https://i.sstatic.net/...
<p>You can also select your default terminal by pressing <kbd>F1</kbd> in Visual Studio Code and typing/selecting <strong><code>Terminal: Select Default Profile</code></strong> (or <strong><code>Terminal: Select Default Shell</code></strong> in older Visual Studio Code versions).</p> <p><img src="https://i.sstatic.net/...
387, 391, 526, 2631, 111608
bash, cmd, powershell, terminal, visual-studio-code
<h1>Change the default terminal in Visual Studio Code</h1> <p>I am using Visual Studio Code on my Windows 10 PC. I want to change my default terminal from Windows <a href="https://en.wikipedia.org/wiki/PowerShell" rel="noreferrer">PowerShell</a> to <a href="https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="nore...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
167
bash
# Change the default terminal in Visual Studio Code I am using Visual Studio Code on my Windows 10 PC. I want to change my default terminal from Windows [PowerShell](https://en.wikipedia.org/wiki/PowerShell) to [Bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) on [Ubuntu](https://en.wikipedia.org/wiki/Ubuntu...
You can also select your default terminal by pressing `F1` in Visual Studio Code and typing/selecting **`Terminal: Select Default Profile`** (or **`Terminal: Select Default Shell`** in older Visual Studio Code versions). ![Terminal Selection 3](https://i.sstatic.net/KHWog.png) Older: ![Terminal Selection](https://i....
31579509
Can pm2 run an 'npm start' script
452
2015-07-23 06:18:02
<p>Is there a way for pm2 to run an npm start script or do you just have to run <code>pm2 start app.js</code></p> <p>So in development</p> <pre><code>npm start </code></pre> <p>Then in production with pm2 you would run something like</p> <pre><code>pm2 start 'npm start' </code></pre> <p>There is an equivalent way to do...
685,132
3,709,775
2025-07-24 07:45:14
37,775,318
904
2016-06-12 14:23:05
3,314,614
2020-06-14 12:38:30
https://stackoverflow.com/q/31579509
https://stackoverflow.com/a/37775318
<p>PM2 now supports npm start:</p> <pre><code>pm2 start npm -- start </code></pre> <p>To assign a name to the PM2 process, use the <code>--name</code> option:</p> <pre><code>pm2 start npm --name "app name" -- start </code></pre>
<p>PM2 now supports npm start:</p> <pre><code>pm2 start npm -- start </code></pre> <p>To assign a name to the PM2 process, use the <code>--name</code> option:</p> <pre><code>pm2 start npm --name "app name" -- start </code></pre>
390, 46426, 61387, 99738
node.js, npm, pm2, shell
<h1>Can pm2 run an 'npm start' script</h1> <p>Is there a way for pm2 to run an npm start script or do you just have to run <code>pm2 start app.js</code></p> <p>So in development</p> <pre><code>npm start </code></pre> <p>Then in production with pm2 you would run something like</p> <pre><code>pm2 start 'npm start' </code...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
168
bash
# Can pm2 run an 'npm start' script Is there a way for pm2 to run an npm start script or do you just have to run `pm2 start app.js` So in development ``` npm start ``` Then in production with pm2 you would run something like ``` pm2 start 'npm start' ``` There is an equivalent way to do this in `forever`: ``` fo...
PM2 now supports npm start: ``` pm2 start npm -- start ``` To assign a name to the PM2 process, use the `--name` option: ``` pm2 start npm --name "app name" -- start ```
669452
Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash?
958
2009-03-21 15:28:45
<p>A coworker claimed recently in a code review that the <code>[[ ]]</code> construct is to be preferred over <code>[ ]</code> in constructs like</p> <pre><code>if [ &quot;`id -nu`&quot; = &quot;$someuser&quot; ] ; then echo &quot;I love you madly, $someuser&quot; fi </code></pre> <p>He couldn't provide a rational...
441,996
10,888
2025-08-04 04:52:53
669,486
900
2009-03-21 15:44:09
34,509
2025-03-09 02:12:13
https://stackoverflow.com/q/669452
https://stackoverflow.com/a/669486
<p><code>[[</code> has fewer surprises and is generally safer to use. But it is not portable - POSIX doesn't specify what it does and only some shells support it (beside bash, I heard ksh supports it too). For example, you can do</p> <pre><code>[[ -e $b ]] </code></pre> <p>to test whether a file exists. But with <code>...
<p><code>[[</code> has fewer surprises and is generally safer to use. But it is not portable - POSIX doesn't specify what it does and only some shells support it (beside bash, I heard ksh supports it too). For example, you can do</p> <pre><code>[[ -e $b ]] </code></pre> <p>to test whether a file exists. But with <code>...
367, 387, 2773
bash, if-statement, syntax
<h1>Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash?</h1> <p>A coworker claimed recently in a code review that the <code>[[ ]]</code> construct is to be preferred over <code>[ ]</code> in constructs like</p> <pre><code>if [ &quot;`id -nu`&quot; = &quot;$someuser&quot; ] ; then e...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
169
bash
# Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash? A coworker claimed recently in a code review that the `[[ ]]` construct is to be preferred over `[ ]` in constructs like ``` if [ "`id -nu`" = "$someuser" ] ; then echo "I love you madly, $someuser" fi ``` He couldn't provide...
`[[` has fewer surprises and is generally safer to use. But it is not portable - POSIX doesn't specify what it does and only some shells support it (beside bash, I heard ksh supports it too). For example, you can do ``` [[ -e $b ]] ``` to test whether a file exists. But with `[`, you have to wrap `$b` in double-quote...
56839307
Adding Git-Bash to the new Windows Terminal
606
2019-07-01 16:38:57
<p>I'm trying to add a new terminal (Git Bash) to the new Windows Terminal. However, I can't get it to work.</p> <p>I tried changing the <code>commandline</code> property in the <code>profiles</code> array to <code>git-bash.exe</code> but no luck.</p> <p>Does anyone have an idea how to get this to work?</p>
384,671
5,253,155
2025-05-12 19:40:17
57,369,284
891
2019-08-06 05:09:44
242,042
2024-01-05 06:12:04
https://stackoverflow.com/q/56839307
https://stackoverflow.com/a/57369284
<h1>Overview</h1> <ol> <li>Open settings with <kbd>Ctrl</kbd>+<kbd>,</kbd></li> <li>You'll want to append one of the profiles options below (depending on what version of git you have installed) to the <code>&quot;list&quot;:</code> portion of the <code>settings.json</code> file:</li> </ol> <p><a href="https://i.sstatic...
<h1>Overview</h1> <ol> <li>Open settings with <kbd>Ctrl</kbd>+<kbd>,</kbd></li> <li>You'll want to append one of the profiles options below (depending on what version of git you have installed) to the <code>&quot;list&quot;:</code> portion of the <code>settings.json</code> file:</li> </ol> <p><a href="https://i.sstatic...
61874, 138178
git-bash, windows-terminal
<h1>Adding Git-Bash to the new Windows Terminal</h1> <p>I'm trying to add a new terminal (Git Bash) to the new Windows Terminal. However, I can't get it to work.</p> <p>I tried changing the <code>commandline</code> property in the <code>profiles</code> array to <code>git-bash.exe</code> but no luck.</p> <p>Does anyone ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
170
bash
# Adding Git-Bash to the new Windows Terminal I'm trying to add a new terminal (Git Bash) to the new Windows Terminal. However, I can't get it to work. I tried changing the `commandline` property in the `profiles` array to `git-bash.exe` but no luck. Does anyone have an idea how to get this to work?
# Overview 1. Open settings with `Ctrl`+`,` 2. You'll want to append one of the profiles options below (depending on what version of git you have installed) to the `"list":` portion of the `settings.json` file: [![Open settings.json in Windows Terminal sidebar](https://i.sstatic.net/kl4Kz.png)](https://i.sstatic.net/...
43158140
Way to create multiline comments in Bash?
495
2017-04-01 14:33:08
<p>I have recently started studying shell script and I'd like to be able to comment out a set of lines in a shell script. I mean like it is in case of C/Java :</p> <pre><code>/* comment1 comment2 comment3 */ </code></pre> <p>How could I do that?</p>
480,418
7,310,822
2025-07-04 14:45:16
43,158,193
879
2017-04-01 14:38:15
7,022,822
2019-01-13 18:02:43
https://stackoverflow.com/q/43158140
https://stackoverflow.com/a/43158193
<p>Use <code>: '</code> to open and <code>'</code> to close.</p> <p>For example:</p> <pre><code>: ' This is a very neat comment in bash ' </code></pre>
<p>Use <code>: '</code> to open and <code>'</code> to close.</p> <p>For example:</p> <pre><code>: ' This is a very neat comment in bash ' </code></pre>
390, 1966, 10941
comments, multiline, shell
<h1>Way to create multiline comments in Bash?</h1> <p>I have recently started studying shell script and I'd like to be able to comment out a set of lines in a shell script. I mean like it is in case of C/Java :</p> <pre><code>/* comment1 comment2 comment3 */ </code></pre> <p>How could I do that?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
171
bash
# Way to create multiline comments in Bash? I have recently started studying shell script and I'd like to be able to comment out a set of lines in a shell script. I mean like it is in case of C/Java : ``` /* comment1 comment2 comment3 */ ``` How could I do that?
Use `: '` to open and `'` to close. For example: ``` : ' This is a very neat comment in bash ' ```
42950501
Delete node_modules folder recursively from a specified path using command line
388
2017-03-22 11:34:57
<p>I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the <em><a href="https://stackoverflow.com/questions/63294260/what-is-the-purpose-of-the-node-modules-folder">node_modules</a></em> folder, as it is taking a lot of space and can also be retrieved any time using...
170,199
5,007,098
2025-11-06 14:54:49
43,561,012
874
2017-04-22 15:29:02
1,293,700
2022-11-12 00:30:08
https://stackoverflow.com/q/42950501
https://stackoverflow.com/a/43561012
<p>Print out a list of directories to be deleted:</p> <pre class="lang-none prettyprint-override"><code>find . -name 'node_modules' -type d -prune </code></pre> <p>Delete directories from the current working directory:</p> <pre class="lang-none prettyprint-override"><code>find . -name 'node_modules' -type d -prune -exe...
<p>Print out a list of directories to be deleted:</p> <pre class="lang-none prettyprint-override"><code>find . -name 'node_modules' -type d -prune </code></pre> <p>Delete directories from the current working directory:</p> <pre class="lang-none prettyprint-override"><code>find . -name 'node_modules' -type d -prune -exe...
369, 387, 11225, 78230
bash, command-line-interface, macos, node-modules
<h1>Delete node_modules folder recursively from a specified path using command line</h1> <p>I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the <em><a href="https://stackoverflow.com/questions/63294260/what-is-the-purpose-of-the-node-modules-folder">node_modules...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
172
bash
# Delete node_modules folder recursively from a specified path using command line I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the *[node_modules](https://stackoverflow.com/questions/63294260/what-is-the-purpose-of-the-node-modules-folder)* folder, as it is ...
Print out a list of directories to be deleted: ``` find . -name 'node_modules' -type d -prune ``` Delete directories from the current working directory: ``` find . -name 'node_modules' -type d -prune -exec rm -rf '{}' + ``` --- Alternatively you can use [trash](https://github.com/sindresorhus/trash-cli) (`brew ins...
12667797
Using cURL to upload POST data with files
604
2012-10-01 05:10:56
<p>I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. How should I go about doing that ?</p> <p>HTTP Post parameters:</p> <p>userid = 12345 filecomment = This is an image file</p> <p>HTTP File upload: File location = /home/user1/Desktop/test.jpg F...
1,070,605
476,467
2024-07-15 11:49:02
12,667,839
871
2012-10-01 05:18:26
791,265
2015-11-05 12:44:57
https://stackoverflow.com/q/12667797
https://stackoverflow.com/a/12667839
<p>You need to use the <code>-F</code> option:<br> <code>-F/--form &lt;name=content&gt; Specify HTTP multipart POST data (H)</code></p> <p>Try this:</p> <pre><code>curl \ -F "userid=1" \ -F "filecomment=This is an image file" \ -F "image=@/home/user1/Desktop/test.jpg" \ localhost/uploader.php </code></pre>
<p>You need to use the <code>-F</code> option:<br> <code>-F/--form &lt;name=content&gt; Specify HTTP multipart POST data (H)</code></p> <p>Try this:</p> <pre><code>curl \ -F "userid=1" \ -F "filecomment=This is an image file" \ -F "image=@/home/user1/Desktop/test.jpg" \ localhost/uploader.php </code></pre>
390, 904, 1554, 4209, 5310
curl, file, file-upload, post, shell
<h1>Using cURL to upload POST data with files</h1> <p>I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. How should I go about doing that ?</p> <p>HTTP Post parameters:</p> <p>userid = 12345 filecomment = This is an image file</p> <p>HTTP File upl...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
173
bash
# Using cURL to upload POST data with files I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific form name. How should I go about doing that ? HTTP Post parameters: userid = 12345 filecomment = This is an image file HTTP File upload: File location = /home/use...
You need to use the `-F` option: `-F/--form <name=content> Specify HTTP multipart POST data (H)` Try this: ``` curl \ -F "userid=1" \ -F "filecomment=This is an image file" \ -F "image=@/home/user1/Desktop/test.jpg" \ localhost/uploader.php ```
2920416
./configure : /bin/sh^M : bad interpreter
453
2010-05-27 10:49:52
<p>I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error</p> <pre><code>[root@localhost lpng142]# ./configure bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory [root@localhost lpng142]# </code></pre> <p>How do I fix this? The <code>/etc/fstab</co...
612,539
348,862
2022-10-12 15:17:51
5,514,351
871
2011-04-01 13:53:41
687,701
2018-03-30 20:17:38
https://stackoverflow.com/q/2920416
https://stackoverflow.com/a/5514351
<p>To fix, open your script with vi or vim and enter in vi command mode (key <KBD>Esc</KBD>), then type this:</p> <pre><code>:set fileformat=unix </code></pre> <p>Finally save it</p> <p><code>:x!</code> or <code>:wq!</code></p>
<p>To fix, open your script with vi or vim and enter in vi command mode (key <KBD>Esc</KBD>), then type this:</p> <pre><code>:set fileformat=unix </code></pre> <p>Finally save it</p> <p><code>:x!</code> or <code>:wq!</code></p>
58, 387, 390, 3705, 14899
bash, carriage-return, linux, newline, shell
<h1>./configure : /bin/sh^M : bad interpreter</h1> <p>I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error</p> <pre><code>[root@localhost lpng142]# ./configure bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory [root@localhost lpng142]# </code></p...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
174
bash
# ./configure : /bin/sh^M : bad interpreter I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error ``` [root@localhost lpng142]# ./configure bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory [root@localhost lpng142]# ``` How do I fix this? The `/e...
To fix, open your script with vi or vim and enter in vi command mode (key `Esc`), then type this: ``` :set fileformat=unix ``` Finally save it `:x!` or `:wq!`
10610327
Delete all local Git branches
770
2012-05-15 23:45:47
<p>I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into <em>master</em> and then push.</p> <p>What tends to happen over time due to a combination of laziness or forgetfulness, is that I end up with a large list of local branches, som...
743,284
629,555
2025-10-02 14:48:43
10,610,669
870
2012-05-16 00:43:24
1,286,639
2022-07-19 10:18:53
https://stackoverflow.com/q/10610327
https://stackoverflow.com/a/10610669
<p>The 'git branch -d' subcommand can delete more than one branch. So, simplifying @sblom's answer but adding a critical xargs:</p> <pre><code>git branch -D `git branch --merged | grep -v \* | xargs` </code></pre> <p>or, further simplified to:</p> <pre><code>git branch --merged | grep -v \* | xargs git branch -D </co...
<p>The 'git branch -d' subcommand can delete more than one branch. So, simplifying @sblom's answer but adding a critical xargs:</p> <pre><code>git branch -D `git branch --merged | grep -v \* | xargs` </code></pre> <p>or, further simplified to:</p> <pre><code>git branch --merged | grep -v \* | xargs git branch -D </co...
119, 387, 456, 549, 1271
bash, git, grep, ubuntu, version-control
<h1>Delete all local Git branches</h1> <p>I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into <em>master</em> and then push.</p> <p>What tends to happen over time due to a combination of laziness or forgetfulness, is that I end up w...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
175
bash
# Delete all local Git branches I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into *master* and then push. What tends to happen over time due to a combination of laziness or forgetfulness, is that I end up with a large list of lo...
The 'git branch -d' subcommand can delete more than one branch. So, simplifying @sblom's answer but adding a critical xargs: ``` git branch -D `git branch --merged | grep -v \* | xargs` ``` or, further simplified to: ``` git branch --merged | grep -v \* | xargs git branch -D ``` Importantly, as [noted by @AndrewC](...
2664740
Extract file basename without path and extension in bash
490
2010-04-19 01:25:28
<p>Given file names like these:</p> <pre><code>/the/path/foo.txt bar.txt </code></pre> <p>I hope to get:</p> <pre><code>foo bar </code></pre> <p>Why this doesn't work?</p> <pre><code>#!/bin/bash fullfile=$1 fname=$(basename $fullfile) fbname=${fname%.*} echo $fbname </code></pre> <p>What's the right way to do it...
745,989
67,405
2023-07-06 21:40:37
2,664,746
870
2010-04-19 01:27:14
131,527
2020-01-10 07:52:19
https://stackoverflow.com/q/2664740
https://stackoverflow.com/a/2664746
<p>You don't have to call the external <code>basename</code> command. Instead, you could use the following commands:</p> <pre><code>$ s=/the/path/foo.txt $ echo "${s##*/}" foo.txt $ s=${s##*/} $ echo "${s%.txt}" foo $ echo "${s%.*}" foo </code></pre> <p>Note that this solution should work in all recent (<em>post 2004...
<p>You don't have to call the external <code>basename</code> command. Instead, you could use the following commands:</p> <pre><code>$ s=/the/path/foo.txt $ echo "${s##*/}" foo.txt $ s=${s##*/} $ echo "${s%.txt}" foo $ echo "${s%.*}" foo </code></pre> <p>Note that this solution should work in all recent (<em>post 2004...
34, 58, 387, 1062
bash, filenames, linux, unix
<h1>Extract file basename without path and extension in bash</h1> <p>Given file names like these:</p> <pre><code>/the/path/foo.txt bar.txt </code></pre> <p>I hope to get:</p> <pre><code>foo bar </code></pre> <p>Why this doesn't work?</p> <pre><code>#!/bin/bash fullfile=$1 fname=$(basename $fullfile) fbname=${fnam...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
176
bash
# Extract file basename without path and extension in bash Given file names like these: ``` /the/path/foo.txt bar.txt ``` I hope to get: ``` foo bar ``` Why this doesn't work? ``` #!/bin/bash fullfile=$1 fname=$(basename $fullfile) fbname=${fname%.*} echo $fbname ``` What's the right way to do it?
You don't have to call the external `basename` command. Instead, you could use the following commands: ``` $ s=/the/path/foo.txt $ echo "${s##*/}" foo.txt $ s=${s##*/} $ echo "${s%.txt}" foo $ echo "${s%.*}" foo ``` Note that this solution should work in all recent (*post 2004*) *POSIX* compliant shells, (e.g. `bash`...
3427872
What's the difference between [ and [[ in Bash?
584
2010-08-06 21:18:21
<p>I looked at bash man page and the <code>[[</code> says it uses Conditional Expressions. Then I looked at Conditional Expressions section and it lists the same operators as <code>test</code> (and <code>[</code>).</p> <p>So I wonder, what is the difference between <code>[</code> and <code>[[</code> in Bash?</p>
268,067
257,942
2024-03-30 19:27:33
3,427,931
869
2010-08-06 21:29:08
68,587
2017-10-12 15:20:59
https://stackoverflow.com/q/3427872
https://stackoverflow.com/a/3427931
<p><code>[[</code> is bash's improvement to the <code>[</code> command. It has several enhancements that make it a better choice if you write scripts that target bash. My favorites are:</p> <ol> <li><p>It is a syntactical feature of the shell, so it has some special behavior that <code>[</code> doesn't have. You no lo...
<p><code>[[</code> is bash's improvement to the <code>[</code> command. It has several enhancements that make it a better choice if you write scripts that target bash. My favorites are:</p> <ol> <li><p>It is a syntactical feature of the shell, so it has some special behavior that <code>[</code> doesn't have. You no lo...
387
bash
<h1>What's the difference between [ and [[ in Bash?</h1> <p>I looked at bash man page and the <code>[[</code> says it uses Conditional Expressions. Then I looked at Conditional Expressions section and it lists the same operators as <code>test</code> (and <code>[</code>).</p> <p>So I wonder, what is the difference betw...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
177
bash
# What's the difference between [ and [[ in Bash? I looked at bash man page and the `[[` says it uses Conditional Expressions. Then I looked at Conditional Expressions section and it lists the same operators as `test` (and `[`). So I wonder, what is the difference between `[` and `[[` in Bash?
`[[` is bash's improvement to the `[` command. It has several enhancements that make it a better choice if you write scripts that target bash. My favorites are: 1. It is a syntactical feature of the shell, so it has some special behavior that `[` doesn't have. You no longer have to quote variables like mad because `[[...
41618766
Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel
421
2017-01-12 16:52:33
<p>I'm trying to execute this powershell command</p> <p><code>Invoke-WebRequest -Uri https://apod.nasa.gov/apod/</code></p> <p>and I get this error. <em>"Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel."</em> https requests appear to work ("<a href="https://google.com" rel="noref...
493,191
118,889
2021-03-13 13:27:02
41,618,979
869
2017-01-12 17:03:45
1,530,987
2017-01-12 17:03:45
https://stackoverflow.com/q/41618766
https://stackoverflow.com/a/41618979
<p>try using this one</p> <pre><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://apod.nasa.gov/apod/ </code></pre>
<p>try using this one</p> <pre><code>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://apod.nasa.gov/apod/ </code></pre>
526, 641
powershell, ssl
<h1>Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel</h1> <p>I'm trying to execute this powershell command</p> <p><code>Invoke-WebRequest -Uri https://apod.nasa.gov/apod/</code></p> <p>and I get this error. <em>"Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel."</em> ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
178
bash
# Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel I'm trying to execute this powershell command `Invoke-WebRequest -Uri https://apod.nasa.gov/apod/` and I get this error. *"Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel."* https requests appear to work ("<https://...
try using this one ``` [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://apod.nasa.gov/apod/ ```
4749330
How to test if string exists in file with Bash?
463
2011-01-20 15:55:31
<p>I have a file that contains directory names:</p> <p><code>my_list.txt</code> :</p> <pre><code>/tmp /var/tmp </code></pre> <p>I'd like to check in Bash before I'll add a directory name if that name already exists in the file.</p>
914,377
410,999
2020-12-09 21:08:49
4,749,368
868
2011-01-20 15:58:40
14,637
2019-09-07 18:00:32
https://stackoverflow.com/q/4749330
https://stackoverflow.com/a/4749368
<pre><code>grep -Fxq &quot;$FILENAME&quot; my_list.txt </code></pre> <p>The exit status is 0 (true) if the name was found, 1 (false) if not, so:</p> <pre><code>if grep -Fxq &quot;$FILENAME&quot; my_list.txt then # code if found else # code if not found fi </code></pre> <h3>Explanation</h3> <p>Here are the relev...
<pre><code>grep -Fxq &quot;$FILENAME&quot; my_list.txt </code></pre> <p>The exit status is 0 (true) if the name was found, 1 (false) if not, so:</p> <pre><code>if grep -Fxq &quot;$FILENAME&quot; my_list.txt then # code if found else # code if not found fi </code></pre> <h3>Explanation</h3> <p>Here are the relev...
139, 387, 5310
bash, file, string
<h1>How to test if string exists in file with Bash?</h1> <p>I have a file that contains directory names:</p> <p><code>my_list.txt</code> :</p> <pre><code>/tmp /var/tmp </code></pre> <p>I'd like to check in Bash before I'll add a directory name if that name already exists in the file.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
179
bash
# How to test if string exists in file with Bash? I have a file that contains directory names: `my_list.txt` : ``` /tmp /var/tmp ``` I'd like to check in Bash before I'll add a directory name if that name already exists in the file.
``` grep -Fxq "$FILENAME" my_list.txt ``` The exit status is 0 (true) if the name was found, 1 (false) if not, so: ``` if grep -Fxq "$FILENAME" my_list.txt then # code if found else # code if not found fi ``` ### Explanation Here are the relevant sections of [the man page for `grep`](https://linux.die.net/m...
9587445
How to create a link to a directory on linux
485
2012-03-06 16:08:40
<p>How to create a link to an existing file or directory using a GNU Linux shell command?</p>
983,547
1,104,696
2024-01-16 23:39:18
9,587,490
867
2012-03-06 16:10:47
1,118,101
2024-01-16 23:39:18
https://stackoverflow.com/q/9587445
https://stackoverflow.com/a/9587490
<p>Symbolic or soft link (files or directories, more flexible and self documenting)</p> <pre><code># Source Link ln -s /home/jake/doc/test/2000/something /home/jake/xxx </code></pre> <p>Hard link (files only, less flexible and not self documenting)</p> <pre><code># Source ...
<p>Symbolic or soft link (files or directories, more flexible and self documenting)</p> <pre><code># Source Link ln -s /home/jake/doc/test/2000/something /home/jake/xxx </code></pre> <p>Hard link (files only, less flexible and not self documenting)</p> <pre><code># Source ...
58, 390, 1796
command, linux, shell
<h1>How to create a link to a directory on linux</h1> <p>How to create a link to an existing file or directory using a GNU Linux shell command?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
180
bash
# How to create a link to a directory on linux How to create a link to an existing file or directory using a GNU Linux shell command?
Symbolic or soft link (files or directories, more flexible and self documenting) ``` # Source Link ln -s /home/jake/doc/test/2000/something /home/jake/xxx ``` Hard link (files only, less flexible and not self documenting) ``` # Source Link ln /home/jake/d...
820517
.bashrc at ssh login
501
2009-05-04 15:10:11
<p>When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my <code>.bashrc</code> are not set.</p> <p>If I do a source <code>.bashrc</code>, the variables are properly set, and all is well.</p> <p>How come <code>.bashrc</code> isn't run at login?</p>
216,083
339,046
2024-05-23 20:45:42
820,533
866
2009-05-04 15:14:39
40,005
2009-05-04 15:14:39
https://stackoverflow.com/q/820517
https://stackoverflow.com/a/820533
<p><code>.bashrc</code> is not sourced when you log in using SSH. You need to source it in your <code>.bash_profile</code> like this:</p> <pre><code>if [ -f ~/.bashrc ]; then . ~/.bashrc fi </code></pre>
<p><code>.bashrc</code> is not sourced when you log in using SSH. You need to source it in your <code>.bash_profile</code> like this:</p> <pre><code>if [ -f ~/.bashrc ]; then . ~/.bashrc fi </code></pre>
386, 387, 549
bash, ssh, ubuntu
<h1>.bashrc at ssh login</h1> <p>When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my <code>.bashrc</code> are not set.</p> <p>If I do a source <code>.bashrc</code>, the variables are properly set, and all is well.</p> <p>How come <code>.bashrc</code> isn't run at login?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
181
bash
# .bashrc at ssh login When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my `.bashrc` are not set. If I do a source `.bashrc`, the variables are properly set, and all is well. How come `.bashrc` isn't run at login?
`.bashrc` is not sourced when you log in using SSH. You need to source it in your `.bash_profile` like this: ``` if [ -f ~/.bashrc ]; then . ~/.bashrc fi ```
14219092
Bash script – "/bin/bash^M: bad interpreter: No such file or directory"
870
2013-01-08 16:03:35
<p>I'm using <a href="http://linuxcommand.org/lc3_writing_shell_scripts.php" rel="noreferrer">this tutorial</a> to learn bash scripts to automate a few tasks for me.<br /> I'm connecting to a server using putty.</p> <p>The script, located in <code>.../Documents/LOG</code>, is:</p> <pre><code>#!/bin/bash # My first scri...
1,379,886
350,668
2023-07-31 05:29:48
14,219,160
863
2013-01-08 16:07:24
992,151
2018-10-04 09:09:41
https://stackoverflow.com/q/14219092
https://stackoverflow.com/a/14219160
<p>I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.</p> <p>Try running <code>dos2unix</code> on the script:</p> <p><a href="http://dos2unix.sourceforge.net/" rel="noreferrer">http://dos2unix.sourceforge.net/</a></p> <p>Or just rewrite the script in your...
<p>I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.</p> <p>Try running <code>dos2unix</code> on the script:</p> <p><a href="http://dos2unix.sourceforge.net/" rel="noreferrer">http://dos2unix.sourceforge.net/</a></p> <p>Or just rewrite the script in your...
387, 3705, 14899, 47852
bash, carriage-return, linefeed, newline
<h1>Bash script – "/bin/bash^M: bad interpreter: No such file or directory"</h1> <p>I'm using <a href="http://linuxcommand.org/lc3_writing_shell_scripts.php" rel="noreferrer">this tutorial</a> to learn bash scripts to automate a few tasks for me.<br /> I'm connecting to a server using putty.</p> <p>The script, located ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
182
bash
# Bash script – "/bin/bash^M: bad interpreter: No such file or directory" I'm using [this tutorial](http://linuxcommand.org/lc3_writing_shell_scripts.php) to learn bash scripts to automate a few tasks for me. I'm connecting to a server using putty. The script, located in `.../Documents/LOG`, is: ``` #!/bin/bash # ...
I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment. Try running `dos2unix` on the script: <http://dos2unix.sourceforge.net/> Or just rewrite the script in your Unix env using `vi` and test. Unix uses different line endings so can't read the file you create...
714877
Setting Windows PowerShell environment variables
1,071
2009-04-03 17:19:35
<p>I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the environment variables for PowerShell (v1)?</p> <p>Note:</p> <p>I want to make my changes permanent, so I don't have to set it every time I run ...
1,483,203
7,883
2025-07-08 10:20:30
714,918
854
2009-04-03 17:35:44
23,283
2023-04-16 20:35:27
https://stackoverflow.com/q/714877
https://stackoverflow.com/a/714918
<p>Changing the actual environment variables can be done by using the <code>env: namespace / drive</code> information. For example, this code will update the path environment variable:</p> <pre><code>$env:PATH = &quot;SomeRandomPath&quot;; (replaces existing path) $env:PATH += &quot;;SomeRandomPath&quot; ...
<p>Changing the actual environment variables can be done by using the <code>env: namespace / drive</code> information. For example, this code will update the path environment variable:</p> <pre><code>$env:PATH = &quot;SomeRandomPath&quot;; (replaces existing path) $env:PATH += &quot;;SomeRandomPath&quot; ...
64, 526
powershell, windows
<h1>Setting Windows PowerShell environment variables</h1> <p>I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the environment variables for PowerShell (v1)?</p> <p>Note:</p> <p>I want to make my chan...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
183
bash
# Setting Windows PowerShell environment variables I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the environment variables for PowerShell (v1)? Note: I want to make my changes permanent, so I d...
Changing the actual environment variables can be done by using the `env: namespace / drive` information. For example, this code will update the path environment variable: ``` $env:PATH = "SomeRandomPath"; (replaces existing path) $env:PATH += ";SomeRandomPath" (appends to existing path) ``` ##...
6270440
How to test string and integer equality, and combine with logical && and || operators in bash?
339
2011-06-07 19:18:05
<p>I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting):</p> <pre><code>if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2" ) then do something done. </code></pre> <p>And in my failed attempt, I came up with:</p> <pre><co...
425,352
381,798
2024-01-05 08:26:42
6,270,803
854
2011-06-07 19:49:29
387,076
2023-12-19 10:23:24
https://stackoverflow.com/q/6270440
https://stackoverflow.com/a/6270803
<p>What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all.</p> <ul> <li><code>(…)</code> parentheses indicate a <a href="http://www.gnu.org/software/bash/manual/bash.html#Command-Grouping" rel="noreferrer">subshell</a>. What's inside them isn't ...
<p>What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all.</p> <ul> <li><code>(…)</code> parentheses indicate a <a href="http://www.gnu.org/software/bash/manual/bash.html#Command-Grouping" rel="noreferrer">subshell</a>. What's inside them isn't ...
387, 27114
bash, logical-operators
<h1>How to test string and integer equality, and combine with logical && and || operators in bash?</h1> <p>I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting):</p> <pre><code>if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
184
bash
# How to test string and integer equality, and combine with logical && and || operators in bash? I have a couple of variables and I want to check the following condition (written out in words, then my failed attempt at bash scripting): ``` if varA EQUALS 1 AND ( varB EQUALS "t1" OR varB EQUALS "t2" ) then do someth...
What you've written actually almost works (it would work if all the variables were numbers), but it's not an idiomatic way at all. - `(…)` parentheses indicate a [subshell](http://www.gnu.org/software/bash/manual/bash.html#Command-Grouping). What's inside them isn't an expression like in many other languages. It's a l...
1752677
How can I recursively delete an entire directory with PowerShell 2.0?
498
2009-11-17 23:43:39
<p>What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.</p> <p>I have learned from several sources that the most obvious command, <code>Remove-Item $targetDir -Recurse -Force</code>, does not work correctly. This includes a statement ...
721,892
58,525
2025-07-17 21:39:42
1,752,751
852
2009-11-18 00:03:17
73,070
2025-07-17 18:10:28
https://stackoverflow.com/q/1752677
https://stackoverflow.com/a/1752751
<pre class="lang-none prettyprint-override"><code>Remove-Item -Recurse -Force some_directory </code></pre> <p>does indeed work as advertised here.</p> <pre class="lang-none prettyprint-override"><code>rm -r -fo some_directory </code></pre> <p>are shorthand aliases that work too.</p> <p>As far as I understood it, the <c...
<pre class="lang-none prettyprint-override"><code>Remove-Item -Recurse -Force some_directory </code></pre> <p>does indeed work as advertised here.</p> <pre class="lang-none prettyprint-override"><code>rm -r -fo some_directory </code></pre> <p>are shorthand aliases that work too.</p> <p>As far as I understood it, the <c...
64, 99, 526, 34595, 106270
delete-directory, filesystems, powershell, windows, windows-7
<h1>How can I recursively delete an entire directory with PowerShell 2.0?</h1> <p>What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.</p> <p>I have learned from several sources that the most obvious command, <code>Remove-Item $target...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
185
bash
# How can I recursively delete an entire directory with PowerShell 2.0? What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7. I have learned from several sources that the most obvious command, `Remove-Item $targetDir -Recurse -Force`...
``` Remove-Item -Recurse -Force some_directory ``` does indeed work as advertised here. ``` rm -r -fo some_directory ``` are shorthand aliases that work too. As far as I understood it, the `-Recurse` parameter just doesn't work correctly when you try deleting a filtered set of files recursively. For killing a singl...
4774054
Reliable way for a Bash script to get the full path to itself
974
2011-01-23 13:24:16
<p>I have a Bash script that needs to know its full path. I'm trying to find a broadly-compatible way of doing that without ending up with relative or funky-looking paths. I only need to support Bash, not sh, csh, etc.</p> <p>What I've found so far:</p> <ol> <li><p>The accepted answer to <em><a href="https://stackove...
758,568
157,247
2022-06-24 11:18:11
4,774,063
839
2011-01-23 13:25:57
157,247
2021-03-03 15:47:11
https://stackoverflow.com/q/4774054
https://stackoverflow.com/a/4774063
<p>Here's what I've come up with (edit: plus some tweaks provided by <a href="https://stackoverflow.com/users/1430833/sfstewman">sfstewman</a>, <a href="https://stackoverflow.com/users/397210/levigroker">levigroker</a>, <a href="https://stackoverflow.com/users/1858225/kyle-strand">Kyle Strand</a>, and <a href="https://...
<p>Here's what I've come up with (edit: plus some tweaks provided by <a href="https://stackoverflow.com/users/1430833/sfstewman">sfstewman</a>, <a href="https://stackoverflow.com/users/397210/levigroker">levigroker</a>, <a href="https://stackoverflow.com/users/1858225/kyle-strand">Kyle Strand</a>, and <a href="https://...
387, 6268
bash, path
<h1>Reliable way for a Bash script to get the full path to itself</h1> <p>I have a Bash script that needs to know its full path. I'm trying to find a broadly-compatible way of doing that without ending up with relative or funky-looking paths. I only need to support Bash, not sh, csh, etc.</p> <p>What I've found so far...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
186
bash
# Reliable way for a Bash script to get the full path to itself I have a Bash script that needs to know its full path. I'm trying to find a broadly-compatible way of doing that without ending up with relative or funky-looking paths. I only need to support Bash, not sh, csh, etc. What I've found so far: 1. The accept...
Here's what I've come up with (edit: plus some tweaks provided by [sfstewman](https://stackoverflow.com/users/1430833/sfstewman), [levigroker](https://stackoverflow.com/users/397210/levigroker), [Kyle Strand](https://stackoverflow.com/users/1858225/kyle-strand), and [Rob Kennedy](https://stackoverflow.com/users/33732/r...
3737740
Is there a better way to run a command N times in bash?
534
2010-09-17 17:56:25
<p>I occasionally run a bash command line like this:</p> <pre><code>n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done </code></pre> <p>To run <code>some_command</code> a number of times in a row -- 10 times in this case.</p> <p>Often <code>some_command</code> is really a chain of commands or a pipeline.<...
421,977
67,022
2024-02-29 01:41:06
3,737,773
837
2010-09-17 18:01:50
76,310
2021-07-14 23:20:38
https://stackoverflow.com/q/3737740
https://stackoverflow.com/a/3737773
<p>If your range has a variable, <a href="//unix.stackexchange.com/a/460600/17671">use <code>seq</code></a>, like this:</p> <pre><code>count=10 for i in $(seq $count); do command done </code></pre> <p>Simply:</p> <pre><code>for run in {1..10}; do command done </code></pre> <p>Or as a one-liner, for those that wan...
<p>If your range has a variable, <a href="//unix.stackexchange.com/a/460600/17671">use <code>seq</code></a>, like this:</p> <pre><code>count=10 for i in $(seq $count); do command done </code></pre> <p>Simply:</p> <pre><code>for run in {1..10}; do command done </code></pre> <p>Or as a one-liner, for those that wan...
387, 2314
bash, loops
<h1>Is there a better way to run a command N times in bash?</h1> <p>I occasionally run a bash command line like this:</p> <pre><code>n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done </code></pre> <p>To run <code>some_command</code> a number of times in a row -- 10 times in this case.</p> <p>Often <code>...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
187
bash
# Is there a better way to run a command N times in bash? I occasionally run a bash command line like this: ``` n=0; while [[ $n -lt 10 ]]; do some_command; n=$((n+1)); done ``` To run `some_command` a number of times in a row -- 10 times in this case. Often `some_command` is really a chain of commands or a pipelin...
If your range has a variable, [use `seq`](//unix.stackexchange.com/a/460600/17671), like this: ``` count=10 for i in $(seq $count); do command done ``` Simply: ``` for run in {1..10}; do command done ``` Or as a one-liner, for those that want to copy and paste easily: ``` for run in {1..10}; do command; done...
1908610
How to get process ID of background process?
559
2009-12-15 16:24:00
<p>I start a background process from my shell script, and I would like to kill this process when my script finishes.</p> <p>How to get the PID of this process from my shell script? As far as I can see variable <code>$!</code> contains the PID of the current script, not the background process.</p>
426,345
167,739
2024-07-02 14:19:03
1,911,387
834
2009-12-16 00:05:40
23,744
2009-12-16 00:05:40
https://stackoverflow.com/q/1908610
https://stackoverflow.com/a/1911387
<p>You need to save the PID of the background process at the time you start it:</p> <pre><code>foo &amp; FOO_PID=$! # do other stuff kill $FOO_PID </code></pre> <p>You cannot use job control, since that is an interactive feature and tied to a controlling terminal. A script will not necessarily have a terminal attache...
<p>You need to save the PID of the background process at the time you start it:</p> <pre><code>foo &amp; FOO_PID=$! # do other stuff kill $FOO_PID </code></pre> <p>You cannot use job control, since that is an interactive feature and tied to a controlling terminal. A script will not necessarily have a terminal attache...
58, 390, 3300, 15292
background-process, linux, pid, shell
<h1>How to get process ID of background process?</h1> <p>I start a background process from my shell script, and I would like to kill this process when my script finishes.</p> <p>How to get the PID of this process from my shell script? As far as I can see variable <code>$!</code> contains the PID of the current script,...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
188
bash
# How to get process ID of background process? I start a background process from my shell script, and I would like to kill this process when my script finishes. How to get the PID of this process from my shell script? As far as I can see variable `$!` contains the PID of the current script, not the background process...
You need to save the PID of the background process at the time you start it: ``` foo & FOO_PID=$! # do other stuff kill $FOO_PID ``` You cannot use job control, since that is an interactive feature and tied to a controlling terminal. A script will not necessarily have a terminal attached at all so job control will no...
192319
How do I know the script file name in a Bash script?
802
2008-10-10 17:20:41
<p>How can I determine the name of the Bash script file inside the script itself?</p> <p>Like if my script is in file <code>runme.sh</code>, then how would I make it to display "You are running runme.sh" message without hardcoding that?</p>
688,434
20,390
2023-07-13 08:00:03
192,337
833
2008-10-10 17:23:08
23,512
2023-07-12 20:02:57
https://stackoverflow.com/q/192319
https://stackoverflow.com/a/192337
<pre class="lang-bash prettyprint-override"><code>me=$(basename &quot;$0&quot;) </code></pre> <p>For reading through a symlink<sup>1</sup>, which is usually not what you want (you usually don't want to confuse the user this way), try:</p> <pre class="lang-bash prettyprint-override"><code>me=&quot;$(basename &quot;$(tes...
<pre class="lang-bash prettyprint-override"><code>me=$(basename &quot;$0&quot;) </code></pre> <p>For reading through a symlink<sup>1</sup>, which is usually not what you want (you usually don't want to confuse the user this way), try:</p> <pre class="lang-bash prettyprint-override"><code>me=&quot;$(basename &quot;$(tes...
58, 387, 390, 531
bash, linux, scripting, shell
<h1>How do I know the script file name in a Bash script?</h1> <p>How can I determine the name of the Bash script file inside the script itself?</p> <p>Like if my script is in file <code>runme.sh</code>, then how would I make it to display "You are running runme.sh" message without hardcoding that?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
189
bash
# How do I know the script file name in a Bash script? How can I determine the name of the Bash script file inside the script itself? Like if my script is in file `runme.sh`, then how would I make it to display "You are running runme.sh" message without hardcoding that?
``` me=$(basename "$0") ``` For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try: ``` me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" ``` IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!...
3474526
Stop on first error
479
2010-08-13 06:45:49
<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error">Automatic exit from bash shell script on error</a> </p> </blockquote> <p>How can I have bash stop on the first command failure, without putting stuff like...
356,651
149,482
2019-02-02 11:30:14
3,474,556
833
2010-08-13 06:50:49
226,621
2019-02-02 11:30:14
https://stackoverflow.com/q/3474526
https://stackoverflow.com/a/3474556
<p>Maybe you want <code>set -e</code>:</p> <p><a href="http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html" rel="noreferrer">www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181</a>:</p> <blockquote> <p>This tells bash that it should ...
<p>Maybe you want <code>set -e</code>:</p> <p><a href="http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html" rel="noreferrer">www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181</a>:</p> <blockquote> <p>This tells bash that it should ...
387
bash
<h1>Stop on first error</h1> <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error">Automatic exit from bash shell script on error</a> </p> </blockquote> <p>How can I have bash stop on the first command failur...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
190
bash
# Stop on first error > **Possible Duplicate:** > [Automatic exit from bash shell script on error](https://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error) How can I have bash stop on the first command failure, without putting stuff like this all through my code? ``` some_prog ||...
Maybe you want `set -e`: [www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181](http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html): > This tells bash that it should exit the script if any statement returns a non-true return value. The...
19456518
Error when using 'sed' with 'find' command on OS X: "invalid command code ."
360
2013-10-18 18:22:21
<p>Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace.</p> <p>However, with the sed command I'd expect to work:</p> <pre><code>find ./ -type f -exec sed -i "s/192.168.20.1/new...
232,272
143,269
2024-11-03 16:20:04
19,457,213
831
2013-10-18 19:02:18
1,763,614
2016-01-20 22:57:42
https://stackoverflow.com/q/19456518
https://stackoverflow.com/a/19457213
<p>If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of <code>sed</code>, the <code>-i</code> option expects an <code>extension</code> argument so your command is actually parsed as the <code>extension</code> argument and the file path is interpreted as the command code.</p>...
<p>If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of <code>sed</code>, the <code>-i</code> option expects an <code>extension</code> argument so your command is actually parsed as the <code>extension</code> argument and the file path is interpreted as the command code.</p>...
369, 387, 5282, 10193
bash, find, macos, sed
<h1>Error when using 'sed' with 'find' command on OS X: "invalid command code ."</h1> <p>Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace.</p> <p>However, with the sed comman...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
191
bash
# Error when using 'sed' with 'find' command on OS X: "invalid command code ." Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace. However, with the sed command I'd expect to ...
If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of `sed`, the `-i` option expects an `extension` argument so your command is actually parsed as the `extension` argument and the file path is interpreted as the command code. Try adding the `-e` argument explicitly and givin...
5592531
How can I pass an argument to a PowerShell script?
621
2011-04-08 08:33:33
<p>There's a PowerShell script named <code>itunesForward.ps1</code> that makes iTunes fast forward 30 seconds:</p> <pre><code>$iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 } </code></pre> <p>It is executed with a prompt line c...
1,051,878
32,090
2025-04-14 17:30:47
5,592,684
827
2011-04-08 08:48:54
307,138
2022-08-09 19:49:54
https://stackoverflow.com/q/5592531
https://stackoverflow.com/a/5592684
<p>Tested as working:</p> <pre><code>#Must be the first statement in your script (not counting comments) param([Int32]$step=30) $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step } </code></pre> <p>Call it with</p> <pre><code>...
<p>Tested as working:</p> <pre><code>#Must be the first statement in your script (not counting comments) param([Int32]$step=30) $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step } </code></pre> <p>Call it with</p> <pre><code>...
526, 1231, 1362, 4323, 10355
automation, command-line, itunes, parameter-passing, powershell
<h1>How can I pass an argument to a PowerShell script?</h1> <p>There's a PowerShell script named <code>itunesForward.ps1</code> that makes iTunes fast forward 30 seconds:</p> <pre><code>$iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
192
bash
# How can I pass an argument to a PowerShell script? There's a PowerShell script named `itunesForward.ps1` that makes iTunes fast forward 30 seconds: ``` $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 } ``` It is executed wi...
Tested as working: ``` #Must be the first statement in your script (not counting comments) param([Int32]$step=30) $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step } ``` Call it with ``` powershell.exe -file itunesForward....
19306771
How do I get the current user's username in Bash?
582
2013-10-10 21:43:58
<p>How do I get the current user's username in Bash? Do I use <a href="https://linux.die.net/man/1/whoami" rel="noreferrer"><code>whoami</code></a>?</p>
829,509
2,868,901
2025-01-25 22:37:14
19,306,837
825
2013-10-10 21:49:00
1,399,279
2022-05-31 03:47:33
https://stackoverflow.com/q/19306771
https://stackoverflow.com/a/19306837
<p>On the command line, enter</p> <pre><code>whoami </code></pre> <p>or</p> <pre><code>echo &quot;$USER&quot; </code></pre>
<p>On the command line, enter</p> <pre><code>whoami </code></pre> <p>or</p> <pre><code>echo &quot;$USER&quot; </code></pre>
387
bash
<h1>How do I get the current user's username in Bash?</h1> <p>How do I get the current user's username in Bash? Do I use <a href="https://linux.die.net/man/1/whoami" rel="noreferrer"><code>whoami</code></a>?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
193
bash
# How do I get the current user's username in Bash? How do I get the current user's username in Bash? Do I use [`whoami`](https://linux.die.net/man/1/whoami)?
On the command line, enter ``` whoami ``` or ``` echo "$USER" ```
9392735
Linux how to copy but not overwrite?
541
2012-02-22 10:09:35
<p>I want to <code>cp</code> a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?</p>
548,027
940,208
2020-08-27 04:24:29
9,392,784
824
2012-02-22 10:12:11
1,007,273
2016-05-26 18:56:30
https://stackoverflow.com/q/9392735
https://stackoverflow.com/a/9392784
<p>Taken from the <a href="http://manpages.ubuntu.com/manpages/precise/en/man1/cp.1.html" rel="noreferrer">man page</a>:</p> <pre><code>-n, --no-clobber do not overwrite an existing file (overrides a previous -i option) </code></pre> <p>Example:</p> <pre><code>cp -n myoldfile.txt mycopiedfile.txt </cod...
<p>Taken from the <a href="http://manpages.ubuntu.com/manpages/precise/en/man1/cp.1.html" rel="noreferrer">man page</a>:</p> <pre><code>-n, --no-clobber do not overwrite an existing file (overrides a previous -i option) </code></pre> <p>Example:</p> <pre><code>cp -n myoldfile.txt mycopiedfile.txt </cod...
58, 387, 25299
bash, cp, linux
<h1>Linux how to copy but not overwrite?</h1> <p>I want to <code>cp</code> a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
194
bash
# Linux how to copy but not overwrite? I want to `cp` a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noninteractive as this will be a part of a Crontab Bash script. Any ideas?
Taken from the [man page](http://manpages.ubuntu.com/manpages/precise/en/man1/cp.1.html): ``` -n, --no-clobber do not overwrite an existing file (overrides a previous -i option) ``` Example: ``` cp -n myoldfile.txt mycopiedfile.txt ```
13617843
"unary operator expected" error in Bash if condition
384
2012-11-29 02:22:47
<p>This script is getting an error:</p> <pre><code>elif [ $operation = &quot;man&quot; ]; then if [ $aug1 = &quot;add&quot; ]; then # &lt;- Line 75 echo &quot;Man Page for: add&quot; echo &quot;&quot; echo &quot;Syntax: add [number 1] [number 2]&quot; echo &quot;&quot; echo &quot;Descrip...
661,528
1,832,837
2022-07-05 23:33:57
13,618,376
821
2012-11-29 03:31:22
1,566,221
2021-10-05 14:08:41
https://stackoverflow.com/q/13617843
https://stackoverflow.com/a/13618376
<p>If you know you're always going to use Bash, it's much easier to always use the double bracket conditional compound command <code>[[ ... ]]</code>, instead of the POSIX-compatible single bracket version <code>[ ... ]</code>. Inside a <code>[[ ... ]]</code> compound, word-splitting and pathname expansion are not appl...
<p>If you know you're always going to use Bash, it's much easier to always use the double bracket conditional compound command <code>[[ ... ]]</code>, instead of the POSIX-compatible single bracket version <code>[ ... ]</code>. Inside a <code>[[ ... ]]</code> compound, word-splitting and pathname expansion are not appl...
387, 390
bash, shell
<h1>"unary operator expected" error in Bash if condition</h1> <p>This script is getting an error:</p> <pre><code>elif [ $operation = &quot;man&quot; ]; then if [ $aug1 = &quot;add&quot; ]; then # &lt;- Line 75 echo &quot;Man Page for: add&quot; echo &quot;&quot; echo &quot;Syntax: add [number 1]...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
195
bash
# "unary operator expected" error in Bash if condition This script is getting an error: ``` elif [ $operation = "man" ]; then if [ $aug1 = "add" ]; then # <- Line 75 echo "Man Page for: add" echo "" echo "Syntax: add [number 1] [number 2]" echo "" echo "Description:" echo "Add two ...
If you know you're always going to use Bash, it's much easier to always use the double bracket conditional compound command `[[ ... ]]`, instead of the POSIX-compatible single bracket version `[ ... ]`. Inside a `[[ ... ]]` compound, word-splitting and pathname expansion are not applied to words, so you can rely on ``...
2150614
Concatenating multiple text files into a single file in Bash
472
2010-01-27 22:03:28
<p>What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?</p> <p>Currently I'm using windows with cygwin so I have access to BASH.</p> <p>Windows shell command would be nice too but I doubt there is one.</p>
496,244
45,066
2021-11-13 11:25:52
2,150,625
818
2010-01-27 22:04:44
110,088
2010-01-27 23:35:06
https://stackoverflow.com/q/2150614
https://stackoverflow.com/a/2150625
<p>This appends the output to all.txt</p> <pre><code>cat *.txt &gt;&gt; all.txt </code></pre> <p>This overwrites all.txt</p> <pre><code>cat *.txt &gt; all.txt </code></pre>
<p>This appends the output to all.txt</p> <pre><code>cat *.txt &gt;&gt; all.txt </code></pre> <p>This overwrites all.txt</p> <pre><code>cat *.txt &gt; all.txt </code></pre>
387, 390
bash, shell
<h1>Concatenating multiple text files into a single file in Bash</h1> <p>What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?</p> <p>Currently I'm using windows with cygwin so I have access to BASH.</p> <p>Windows shell command would be nice too but I doubt th...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
196
bash
# Concatenating multiple text files into a single file in Bash What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file? Currently I'm using windows with cygwin so I have access to BASH. Windows shell command would be nice too but I doubt there is one.
This appends the output to all.txt ``` cat *.txt >> all.txt ``` This overwrites all.txt ``` cat *.txt > all.txt ```
3953645
Ternary operator (?:) in Bash
807
2010-10-17 14:38:52
<p>Is there a way to do something like this</p> <pre><code>int a = (b == 5) ? c : d; </code></pre> <p>using Bash?</p>
501,290
201,065
2025-12-21 10:30:25
3,953,666
817
2010-10-17 14:42:42
131,527
2025-01-03 02:56:45
https://stackoverflow.com/q/3953645
https://stackoverflow.com/a/3953666
<p>The ternary operator <code>? :</code> is just a short form of <code>if/then/else</code>:</p> <pre><code>case &quot;$b&quot; in 5) a=$c ;; *) a=$d ;; esac </code></pre> <p>Or:</p> <pre><code>[[ $b = 5 ]] &amp;&amp; a=&quot;$c&quot; || a=&quot;$d&quot; </code></pre>
<p>The ternary operator <code>? :</code> is just a short form of <code>if/then/else</code>:</p> <pre><code>case &quot;$b&quot; in 5) a=$c ;; *) a=$d ;; esac </code></pre> <p>Or:</p> <pre><code>[[ $b = 5 ]] &amp;&amp; a=&quot;$c&quot; || a=&quot;$d&quot; </code></pre>
367, 387, 15904
bash, conditional-operator, syntax
<h1>Ternary operator (?:) in Bash</h1> <p>Is there a way to do something like this</p> <pre><code>int a = (b == 5) ? c : d; </code></pre> <p>using Bash?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
197
bash
# Ternary operator (?:) in Bash Is there a way to do something like this ``` int a = (b == 5) ? c : d; ``` using Bash?
The ternary operator `? :` is just a short form of `if/then/else`: ``` case "$b" in 5) a=$c ;; *) a=$d ;; esac ``` Or: ``` [[ $b = 5 ]] && a="$c" || a="$d" ```
6481005
How to obtain the number of CPUs/cores in Linux from the command line?
731
2011-06-25 22:52:34
<p>I have this script, but I do not know how to get the last element in the printout:</p> <pre><code>cat /proc/cpuinfo | awk '/^processor/{print $3}' </code></pre> <p>The last element should be the number of CPUs, minus 1.</p>
855,982
486,720
2024-10-26 12:30:58
6,481,016
814
2011-06-25 22:56:01
349,909
2021-05-23 14:05:25
https://stackoverflow.com/q/6481005
https://stackoverflow.com/a/6481016
<pre><code>grep -c ^processor /proc/cpuinfo </code></pre> <p>will count the number of lines starting with &quot;processor&quot; in <code>/proc/cpuinfo</code></p> <p>For systems with hyper-threading, you can use</p> <pre><code>grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}' </code></pre> <p>which should retur...
<pre><code>grep -c ^processor /proc/cpuinfo </code></pre> <p>will count the number of lines starting with &quot;processor&quot; in <code>/proc/cpuinfo</code></p> <p>For systems with hyper-threading, you can use</p> <pre><code>grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}' </code></pre> <p>which should retur...
58, 387, 448, 23364
bash, cpu, cpu-cores, linux
<h1>How to obtain the number of CPUs/cores in Linux from the command line?</h1> <p>I have this script, but I do not know how to get the last element in the printout:</p> <pre><code>cat /proc/cpuinfo | awk '/^processor/{print $3}' </code></pre> <p>The last element should be the number of CPUs, minus 1.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
198
bash
# How to obtain the number of CPUs/cores in Linux from the command line? I have this script, but I do not know how to get the last element in the printout: ``` cat /proc/cpuinfo | awk '/^processor/{print $3}' ``` The last element should be the number of CPUs, minus 1.
``` grep -c ^processor /proc/cpuinfo ``` will count the number of lines starting with "processor" in `/proc/cpuinfo` For systems with hyper-threading, you can use ``` grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}' ``` which should return (for example) `8` (whereas the command above would return `16`)
1527049
How can I join elements of a Bash array into a delimited string?
628
2009-10-06 17:46:15
<p>If I have an array like this in Bash:</p> <pre><code>FOO=( a b c ) </code></pre> <p>How do I join the elements with commas? For example, producing <code>a,b,c</code>.</p>
417,408
71,522
2024-07-02 11:41:26
17,841,619
814
2013-07-24 18:07:43
789,544
2022-01-14 21:51:08
https://stackoverflow.com/q/1527049
https://stackoverflow.com/a/17841619
<p>A 100% pure Bash function that supports multi-character delimiters is:</p> <pre><code>function join_by { local d=${1-} f=${2-} if shift 2; then printf %s &quot;$f&quot; &quot;${@/#/$d}&quot; fi } </code></pre> <p>For example,</p> <pre><code>join_by , a b c #a,b,c join_by ' , ' a b c #a , b , c join_by ')|(...
<p>A 100% pure Bash function that supports multi-character delimiters is:</p> <pre><code>function join_by { local d=${1-} f=${2-} if shift 2; then printf %s &quot;$f&quot; &quot;${@/#/$d}&quot; fi } </code></pre> <p>For example,</p> <pre><code>join_by , a b c #a,b,c join_by ' , ' a b c #a , b , c join_by ')|(...
114, 387
arrays, bash
<h1>How can I join elements of a Bash array into a delimited string?</h1> <p>If I have an array like this in Bash:</p> <pre><code>FOO=( a b c ) </code></pre> <p>How do I join the elements with commas? For example, producing <code>a,b,c</code>.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
199
bash
# How can I join elements of a Bash array into a delimited string? If I have an array like this in Bash: ``` FOO=( a b c ) ``` How do I join the elements with commas? For example, producing `a,b,c`.
A 100% pure Bash function that supports multi-character delimiters is: ``` function join_by { local d=${1-} f=${2-} if shift 2; then printf %s "$f" "${@/#/$d}" fi } ``` For example, ``` join_by , a b c #a,b,c join_by ' , ' a b c #a , b , c join_by ')|(' a b c #a)|(b)|(c join_by ' %s ' a b c #a %s b %s c jo...
3202111
How can I assign a name for a screen?
536
2010-07-08 09:15:44
<p>I'm using the <a href="https://en.wikipedia.org/wiki/GNU_Screen" rel="noreferrer">Screen</a> multiplexer tool on the command shell and open a lot of screens. I then forget which process ID associates with which task.</p> <p>I would like to set a name for a screen, but I can't find an option in the <a href="https://e...
444,154
131,664
2024-09-16 05:34:19
3,309,696
812
2010-07-22 14:05:36
164,176
2024-09-16 05:34:19
https://stackoverflow.com/q/3202111
https://stackoverflow.com/a/3309696
<h1>To start a new session</h1> <p><code>screen -S your_session_name</code></p> <h1>To rename an existing session</h1> <p><kbd>Ctrl</kbd>+<kbd>a</kbd>, <kbd>:</kbd> <code>sessionname</code> $YOUR_SESSION_NAME <kbd>Enter</kbd></p> <blockquote> <p>You must be inside the session</p> </blockquote> <blockquote> <p><code>ses...
<h1>To start a new session</h1> <p><code>screen -S your_session_name</code></p> <h1>To rename an existing session</h1> <p><kbd>Ctrl</kbd>+<kbd>a</kbd>, <kbd>:</kbd> <code>sessionname</code> $YOUR_SESSION_NAME <kbd>Enter</kbd></p> <blockquote> <p>You must be inside the session</p> </blockquote> <blockquote> <p><code>ses...
34, 58, 390, 5024
gnu-screen, linux, shell, unix
<h1>How can I assign a name for a screen?</h1> <p>I'm using the <a href="https://en.wikipedia.org/wiki/GNU_Screen" rel="noreferrer">Screen</a> multiplexer tool on the command shell and open a lot of screens. I then forget which process ID associates with which task.</p> <p>I would like to set a name for a screen, but I...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
200
bash
# How can I assign a name for a screen? I'm using the [Screen](https://en.wikipedia.org/wiki/GNU_Screen) multiplexer tool on the command shell and open a lot of screens. I then forget which process ID associates with which task. I would like to set a name for a screen, but I can't find an option in the [man page](htt...
# To start a new session `screen -S your_session_name` # To rename an existing session `Ctrl`+`a`, `:` `sessionname` $YOUR_SESSION_NAME `Enter` > You must be inside the session > `sessionname` is command, please type it exactly, not your session name there - yours will be at $YOUR_SESSION_NAME
1550288
OS X Terminal Colors
544
2009-10-11 09:43:01
<p>I just got a Mac after working with Ubuntu Linux for some time. Among the many things I'm trying to figure out is the absence of colors in my terminal window- like the ones that are shown (on Linux) when running <code>ls -la</code> or <code>git status</code>.</p> <p>How can one activate colors in their shell?</p>
526,736
187,907
2024-03-15 01:25:39
5,485,184
810
2011-03-30 10:35:21
411,951
2019-11-20 19:06:03
https://stackoverflow.com/q/1550288
https://stackoverflow.com/a/5485184
<p>Here is a solution I've found to enable the global <a href="http://it.toolbox.com/blogs/lim/how-to-fix-colors-on-mac-osx-terminal-37214" rel="noreferrer">terminal colors</a>.</p> <p>Edit your <code>.bash_profile</code> (since OS X 10.8) — or (for 10.7 and earlier): <code>.profile</code> or <code>.bashrc</code> or <...
<p>Here is a solution I've found to enable the global <a href="http://it.toolbox.com/blogs/lim/how-to-fix-colors-on-mac-osx-terminal-37214" rel="noreferrer">terminal colors</a>.</p> <p>Edit your <code>.bash_profile</code> (since OS X 10.8) — or (for 10.7 and earlier): <code>.profile</code> or <code>.bashrc</code> or <...
369, 390, 391, 6439
colors, macos, shell, terminal
<h1>OS X Terminal Colors</h1> <p>I just got a Mac after working with Ubuntu Linux for some time. Among the many things I'm trying to figure out is the absence of colors in my terminal window- like the ones that are shown (on Linux) when running <code>ls -la</code> or <code>git status</code>.</p> <p>How can one activate...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
201
bash
# OS X Terminal Colors I just got a Mac after working with Ubuntu Linux for some time. Among the many things I'm trying to figure out is the absence of colors in my terminal window- like the ones that are shown (on Linux) when running `ls -la` or `git status`. How can one activate colors in their shell?
Here is a solution I've found to enable the global [terminal colors](http://it.toolbox.com/blogs/lim/how-to-fix-colors-on-mac-osx-terminal-37214). Edit your `.bash_profile` (since OS X 10.8) — or (for 10.7 and earlier): `.profile` or `.bashrc` or `/etc/profile` (depending on availability) — in your home directory and ...
238073
How to add a progress bar to a shell script?
563
2008-10-26 14:32:05
<p>When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.</p> <p>For example, copying a big file, opening a big tar file.</p> <p>What ways do you recommend to add progress bars to shell scripts?</p>
568,860
13,523
2025-11-18 11:29:30
238,094
804
2008-10-26 14:47:53
28,807
2018-09-06 01:42:01
https://stackoverflow.com/q/238073
https://stackoverflow.com/a/238094
<p>You can implement this by overwriting a line. Use <code>\r</code> to go back to the beginning of the line without writing <code>\n</code> to the terminal.</p> <p>Write <code>\n</code> when you're done to advance the line.</p> <p>Use <code>echo -ne</code> to:</p> <ol> <li>not print <code>\n</code> and</li> <li>to...
<p>You can implement this by overwriting a line. Use <code>\r</code> to go back to the beginning of the line without writing <code>\n</code> to the terminal.</p> <p>Write <code>\n</code> when you're done to advance the line.</p> <p>Use <code>echo -ne</code> to:</p> <ol> <li>not print <code>\n</code> and</li> <li>to...
387, 390, 3791, 79778
bash, progress-bar, shell, zsh
<h1>How to add a progress bar to a shell script?</h1> <p>When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.</p> <p>For example, copying a big file, opening a big tar file.</p> <p>What ways do you recommend to add progress bars t...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
202
bash
# How to add a progress bar to a shell script? When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed. For example, copying a big file, opening a big tar file. What ways do you recommend to add progress bars to shell scripts?
You can implement this by overwriting a line. Use `\r` to go back to the beginning of the line without writing `\n` to the terminal. Write `\n` when you're done to advance the line. Use `echo -ne` to: 1. not print `\n` and 2. to recognize escape sequences like `\r`. Here's a demo: ``` echo -ne '##### ...
125281
How do I remove the file suffix and path portion from a path string in Bash?
562
2008-09-24 03:37:00
<p>Given a string file path such as <code>/foo/fizzbuzz.bar</code>, how would I use bash to extract just the <code>fizzbuzz</code> portion of said string?</p>
545,174
1,512
2025-10-15 22:23:18
125,340
801
2008-09-24 03:54:28
13,422
2025-10-15 22:23:18
https://stackoverflow.com/q/125281
https://stackoverflow.com/a/125340
<p>Here's how to do it with the # and % operators in Bash.</p> <pre><code>$ x=&quot;/foo/fizzbuzz.bar&quot; $ y=${x%.bar} $ echo ${y##*/} fizzbuzz </code></pre> <p><code>${x%.bar}</code> could also be <code>${x%.*}</code> to remove everything after the last dot or <code>${x%%.*}</code> to remove everything after the fi...
<p>Here's how to do it with the # and % operators in Bash.</p> <pre><code>$ x=&quot;/foo/fizzbuzz.bar&quot; $ y=${x%.bar} $ echo ${y##*/} fizzbuzz </code></pre> <p><code>${x%.bar}</code> could also be <code>${x%.*}</code> to remove everything after the last dot or <code>${x%%.*}</code> to remove everything after the fi...
139, 387, 1062
bash, filenames, string
<h1>How do I remove the file suffix and path portion from a path string in Bash?</h1> <p>Given a string file path such as <code>/foo/fizzbuzz.bar</code>, how would I use bash to extract just the <code>fizzbuzz</code> portion of said string?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
203
bash
# How do I remove the file suffix and path portion from a path string in Bash? Given a string file path such as `/foo/fizzbuzz.bar`, how would I use bash to extract just the `fizzbuzz` portion of said string?
Here's how to do it with the # and % operators in Bash. ``` $ x="/foo/fizzbuzz.bar" $ y=${x%.bar} $ echo ${y##*/} fizzbuzz ``` `${x%.bar}` could also be `${x%.*}` to remove everything after the last dot or `${x%%.*}` to remove everything after the first dot. Example: ``` $ x="/foo/fizzbuzz.bar.quux" $ y=${x%.*} $ e...
17336915
Return value in a Bash function
644
2013-06-27 07:17:30
<p>I am working with a bash script and I want to execute a function to print a return value:</p> <pre><code>function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } </code></pre> <p>When I execute <code>fun2</code>, it does not print "34". Why is this the case?</p>
909,022
2,496,202
2025-08-05 09:59:53
17,336,953
798
2013-06-27 07:19:19
531,222
2021-03-16 16:08:02
https://stackoverflow.com/q/17336915
https://stackoverflow.com/a/17336953
<p>Although Bash has a <code>return</code> statement, the only thing you can specify with it is the function's own <code>exit</code> status (a value between <code>0</code> and <code>255</code>, 0 meaning &quot;success&quot;). So <code>return</code> is not what you want.</p> <p>You might want to convert your <code>retur...
<p>Although Bash has a <code>return</code> statement, the only thing you can specify with it is the function's own <code>exit</code> status (a value between <code>0</code> and <code>255</code>, 0 meaning &quot;success&quot;). So <code>return</code> is not what you want.</p> <p>You might want to convert your <code>retur...
387, 5569, 9206
bash, function, return-value
<h1>Return value in a Bash function</h1> <p>I am working with a bash script and I want to execute a function to print a return value:</p> <pre><code>function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } </code></pre> <p>When I execute <code>fun2</code>, it does not print "34". Why is this ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
204
bash
# Return value in a Bash function I am working with a bash script and I want to execute a function to print a return value: ``` function fun1(){ return 34 } function fun2(){ local res=$(fun1) echo $res } ``` When I execute `fun2`, it does not print "34". Why is this the case?
Although Bash has a `return` statement, the only thing you can specify with it is the function's own `exit` status (a value between `0` and `255`, 0 meaning "success"). So `return` is not what you want. You might want to convert your `return` statement to an `echo` statement - that way your function output could be ca...
2371248
How to convert timestamps to dates in Bash?
432
2010-03-03 12:41:16
<p>I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns:</p> <pre><code>ts2date 1267619929 </code></pre> <p>and</p> <pre><code>echo 1267619929 | ts2date </code></pre> <p>Both commands sh...
466,989
130,121
2024-09-30 17:24:08
2,371,288
798
2010-03-03 12:47:51
276,138
2022-02-19 23:36:39
https://stackoverflow.com/q/2371248
https://stackoverflow.com/a/2371288
<p>On systems with <a href="https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html" rel="noreferrer">GNU Coreutils</a> &gt;= 5.3.0, e.g. Linux you can use:</p> <pre><code>date -d @1267619929 </code></pre>
<p>On systems with <a href="https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html" rel="noreferrer">GNU Coreutils</a> &gt;= 5.3.0, e.g. Linux you can use:</p> <pre><code>date -d @1267619929 </code></pre>
387, 5002, 32842
bash, date, unix-timestamp
<h1>How to convert timestamps to dates in Bash?</h1> <p>I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns:</p> <pre><code>ts2date 1267619929 </code></pre> <p>and</p> <pre><code>echo 12...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
205
bash
# How to convert timestamps to dates in Bash? I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns: ``` ts2date 1267619929 ``` and ``` echo 1267619929 | ts2date ``` Both commands should...
On systems with [GNU Coreutils](https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html) >= 5.3.0, e.g. Linux you can use: ``` date -d @1267619929 ```
43308319
How can I run bash in a docker container?
422
2017-04-09 15:07:52
<p>I am able to run arbitrary shell commands in a container created from docker/whalesay image.</p> <pre><code>$ docker run docker/whalesay ls -l total 56 -rw-r--r-- 1 root root 931 May 25 2015 ChangeLog ... </code></pre> <p>However, I am unable to run <code>bash</code> in a container created from this image, as the ...
738,669
1,175,080
2026-01-07 03:44:56
43,309,168
788
2017-04-09 16:33:47
2,449,905
2024-05-10 19:44:01
https://stackoverflow.com/q/43308319
https://stackoverflow.com/a/43309168
<p>If you <code>docker run</code> without attaching a tty, and only call <code>bash</code>, then bash finds nothing to do, and it exits. That's because by default, a container is non-interactive, and a shell that runs in non-interactive mode expects a script to run. Absent that, it will exit.</p> <p>To run a disposable...
<p>If you <code>docker run</code> without attaching a tty, and only call <code>bash</code>, then bash finds nothing to do, and it exits. That's because by default, a container is non-interactive, and a shell that runs in non-interactive mode expects a script to run. Absent that, it will exit.</p> <p>To run a disposable...
387, 90304
bash, docker
<h1>How can I run bash in a docker container?</h1> <p>I am able to run arbitrary shell commands in a container created from docker/whalesay image.</p> <pre><code>$ docker run docker/whalesay ls -l total 56 -rw-r--r-- 1 root root 931 May 25 2015 ChangeLog ... </code></pre> <p>However, I am unable to run <code>bash</co...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
206
bash
# How can I run bash in a docker container? I am able to run arbitrary shell commands in a container created from docker/whalesay image. ``` $ docker run docker/whalesay ls -l total 56 -rw-r--r-- 1 root root 931 May 25 2015 ChangeLog ... ``` However, I am unable to run `bash` in a container created from this image...
If you `docker run` without attaching a tty, and only call `bash`, then bash finds nothing to do, and it exits. That's because by default, a container is non-interactive, and a shell that runs in non-interactive mode expects a script to run. Absent that, it will exit. To run a disposable new container, you can simply ...
525872
Echo tab characters in bash script
480
2009-02-08 15:05:27
<p>How do I echo one or more tab characters using a bash script? When I run this code</p> <pre><code>res=' 'x # res = "\t\tx" echo '['$res']' # expect [\t\tx] </code></pre> <p>I get this</p> <pre><code>res=[ x] # that is [&lt;space&gt;x] </code></pre>
718,920
52,141
2022-05-20 18:46:34
525,873
784
2009-02-08 15:08:03
55,925
2016-02-16 07:22:22
https://stackoverflow.com/q/525872
https://stackoverflow.com/a/525873
<pre><code>echo -e ' \t ' </code></pre> <p>will echo 'space tab space newline' (<code>-e</code> means 'enable interpretation of backslash escapes'):</p> <pre><code>$ echo -e ' \t ' | hexdump -C 00000000 20 09 20 0a | . .| </code></pre>
<pre><code>echo -e ' \t ' </code></pre> <p>will echo 'space tab space newline' (<code>-e</code> means 'enable interpretation of backslash escapes'):</p> <pre><code>$ echo -e ' \t ' | hexdump -C 00000000 20 09 20 0a | . .| </code></pre>
387, 1177, 5162, 13824
bash, echo, spaces, tabs
<h1>Echo tab characters in bash script</h1> <p>How do I echo one or more tab characters using a bash script? When I run this code</p> <pre><code>res=' 'x # res = "\t\tx" echo '['$res']' # expect [\t\tx] </code></pre> <p>I get this</p> <pre><code>res=[ x] # that is [&lt;space&gt;x] </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
207
bash
# Echo tab characters in bash script How do I echo one or more tab characters using a bash script? When I run this code ``` res=' 'x # res = "\t\tx" echo '['$res']' # expect [\t\tx] ``` I get this ``` res=[ x] # that is [<space>x] ```
``` echo -e ' \t ' ``` will echo 'space tab space newline' (`-e` means 'enable interpretation of backslash escapes'): ``` $ echo -e ' \t ' | hexdump -C 00000000 20 09 20 0a | . .| ```
696839
How do I write a bash script to restart a process if it dies?
323
2009-03-30 11:27:08
<p>I have a python script that'll be checking a queue and performing an action on each item:</p> <pre><code># checkqueue.py while True: check_queue() do_something() </code></pre> <p>How do I write a bash script that will check if it's running, and if not, start it. Roughly the following pseudo code (or maybe it ...
337,735
3,715
2024-08-31 22:42:37
697,064
782
2009-03-30 12:53:53
58,803
2023-11-22 16:13:22
https://stackoverflow.com/q/696839
https://stackoverflow.com/a/697064
<p>Avoid PID-files, crons, or anything else that tries to evaluate processes that aren't their children.</p> <p>There is a very good reason why in UNIX, you can ONLY wait on your children. Any method (ps parsing, pgrep, storing a PID, ...) that tries to work around that is flawed and has gaping holes in it. Just say ...
<p>Avoid PID-files, crons, or anything else that tries to evaluate processes that aren't their children.</p> <p>There is a very good reason why in UNIX, you can ONLY wait on your children. Any method (ps parsing, pgrep, storing a PID, ...) that tries to work around that is flawed and has gaping holes in it. Just say ...
58, 387, 531, 601
bash, cron, linux, scripting
<h1>How do I write a bash script to restart a process if it dies?</h1> <p>I have a python script that'll be checking a queue and performing an action on each item:</p> <pre><code># checkqueue.py while True: check_queue() do_something() </code></pre> <p>How do I write a bash script that will check if it's running,...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
208
bash
# How do I write a bash script to restart a process if it dies? I have a python script that'll be checking a queue and performing an action on each item: ``` # checkqueue.py while True: check_queue() do_something() ``` How do I write a bash script that will check if it's running, and if not, start it. Roughly th...
Avoid PID-files, crons, or anything else that tries to evaluate processes that aren't their children. There is a very good reason why in UNIX, you can ONLY wait on your children. Any method (ps parsing, pgrep, storing a PID, ...) that tries to work around that is flawed and has gaping holes in it. Just say **no**. In...
7373752
How do I get cURL to not show the progress bar?
845
2011-09-10 18:30:50
<p>I'm trying to use cURL in a script and get it to <strong><em>not</em></strong> show the progress bar. </p> <p>I've tried the <code>-s</code>, <code>-silent</code>, <code>-S</code>, and <code>-quiet</code> options, but none of them work.</p> <p>Here's a typical command I've tried:</p> <pre><code>curl -s http://goo...
576,023
458,961
2024-12-16 21:46:22
7,373,922
781
2011-09-10 19:04:05
190,597
2011-09-10 19:04:05
https://stackoverflow.com/q/7373752
https://stackoverflow.com/a/7373922
<pre><code>curl -s http://google.com &gt; temp.html </code></pre> <p>works for curl version 7.19.5 on Ubuntu 9.10 (no progress bar). But if for some reason that does not work on your platform, you could always redirect stderr to /dev/null:</p> <pre><code>curl http://google.com 2&gt;/dev/null &gt; temp.html </code></...
<pre><code>curl -s http://google.com &gt; temp.html </code></pre> <p>works for curl version 7.19.5 on Ubuntu 9.10 (no progress bar). But if for some reason that does not work on your platform, you could always redirect stderr to /dev/null:</p> <pre><code>curl http://google.com 2&gt;/dev/null &gt; temp.html </code></...
34, 58, 387, 531, 1554
bash, curl, linux, scripting, unix
<h1>How do I get cURL to not show the progress bar?</h1> <p>I'm trying to use cURL in a script and get it to <strong><em>not</em></strong> show the progress bar. </p> <p>I've tried the <code>-s</code>, <code>-silent</code>, <code>-S</code>, and <code>-quiet</code> options, but none of them work.</p> <p>Here's a typic...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
209
bash
# How do I get cURL to not show the progress bar? I'm trying to use cURL in a script and get it to ***not*** show the progress bar. I've tried the `-s`, `-silent`, `-S`, and `-quiet` options, but none of them work. Here's a typical command I've tried: ``` curl -s http://google.com > temp.html ``` I only get the pr...
``` curl -s http://google.com > temp.html ``` works for curl version 7.19.5 on Ubuntu 9.10 (no progress bar). But if for some reason that does not work on your platform, you could always redirect stderr to /dev/null: ``` curl http://google.com 2>/dev/null > temp.html ```
18592173
Select objects based on value of variable in object using jq
549
2013-09-03 12:19:32
<p>I have the following json file:</p> <pre><code>{ &quot;FOO&quot;: { &quot;name&quot;: &quot;Donald&quot;, &quot;location&quot;: &quot;Stockholm&quot; }, &quot;BAR&quot;: { &quot;name&quot;: &quot;Walt&quot;, &quot;location&quot;: &quot;Stockholm&quot; }, &quot;BAZ&...
811,194
179,444
2025-08-08 14:35:33
18,608,100
781
2013-09-04 07:42:01
179,444
2020-05-09 02:40:21
https://stackoverflow.com/q/18592173
https://stackoverflow.com/a/18608100
<p>Adapted from this post on <a href="https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/" rel="noreferrer">Processing JSON with jq</a>, you can use the <a href="https://stedolan.github.io/jq/manual/#select(boolean_expression)" rel="noreferrer"><code>select(bool)</code></a> like this:</p> <pre class="lang...
<p>Adapted from this post on <a href="https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/" rel="noreferrer">Processing JSON with jq</a>, you can use the <a href="https://stedolan.github.io/jq/manual/#select(boolean_expression)" rel="noreferrer"><code>select(bool)</code></a> like this:</p> <pre class="lang...
387, 1151, 1508, 105170
bash, jq, json, select
<h1>Select objects based on value of variable in object using jq</h1> <p>I have the following json file:</p> <pre><code>{ &quot;FOO&quot;: { &quot;name&quot;: &quot;Donald&quot;, &quot;location&quot;: &quot;Stockholm&quot; }, &quot;BAR&quot;: { &quot;name&quot;: &quot;Walt&quot;, ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
210
bash
# Select objects based on value of variable in object using jq I have the following json file: ``` { "FOO": { "name": "Donald", "location": "Stockholm" }, "BAR": { "name": "Walt", "location": "Stockholm" }, "BAZ": { "name": "Jack", "location": "Where...
Adapted from this post on [Processing JSON with jq](https://zerokspot.com/weblog/2013/07/18/processing-json-with-jq/), you can use the [`select(bool)`](https://stedolan.github.io/jq/manual/#select(boolean_expression)) like this: ``` $ jq '.[] | select(.location=="Stockholm")' json { "location": "Stockholm", "name"...
4988226
How do I pass multiple parameters into a function in PowerShell?
608
2011-02-14 01:59:53
<p>If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty.</p> <p>A quick test script:</p> <pre class="lang-none prettyprint-override"><code>Function Test([string]$arg1, [string]$arg2) { Write-H...
984,548
16,522
2025-06-08 15:54:32
4,988,239
780
2011-02-14 02:04:06
6,920
2025-06-08 15:46:57
https://stackoverflow.com/q/4988226
https://stackoverflow.com/a/4988239
<p>Parameters in calls to functions in PowerShell (all versions) <strong>are space-separated, not comma-separated</strong>. Also, the parentheses are entirely unnecessary and will cause a parse error in PowerShell 2.0 (or later) if <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/s...
<p>Parameters in calls to functions in PowerShell (all versions) <strong>are space-separated, not comma-separated</strong>. Also, the parentheses are entirely unnecessary and will cause a parse error in PowerShell 2.0 (or later) if <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/s...
367, 526, 10355
parameter-passing, powershell, syntax
<h1>How do I pass multiple parameters into a function in PowerShell?</h1> <p>If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty.</p> <p>A quick test script:</p> <pre class="lang-none prettyprint...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
211
bash
# How do I pass multiple parameters into a function in PowerShell? If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty. A quick test script: ``` Function Test([string]$arg1, [string]$arg2) { ...
Parameters in calls to functions in PowerShell (all versions) **are space-separated, not comma-separated**. Also, the parentheses are entirely unnecessary and will cause a parse error in PowerShell 2.0 (or later) if [`Set-StrictMode`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-str...
16483119
An example of how to use getopts in bash
611
2013-05-10 13:12:33
<p>I want to call <code>myscript</code> file in this way:</p> <pre><code>$ ./myscript -s 45 -p any_string </code></pre> <p>or</p> <pre><code>$ ./myscript -h #should display help $ ./myscript #should display help </code></pre> <p>My requirements are:</p> <ul> <li><code>getopts</code> here to get the input arguments...
1,038,223
1,003,575
2024-12-10 03:55:18
16,496,491
778
2013-05-11 11:16:27
612,462
2013-08-14 14:58:23
https://stackoverflow.com/q/16483119
https://stackoverflow.com/a/16496491
<pre><code>#!/bin/bash usage() { echo "Usage: $0 [-s &lt;45|90&gt;] [-p &lt;string&gt;]" 1&gt;&amp;2; exit 1; } while getopts ":s:p:" o; do case "${o}" in s) s=${OPTARG} ((s == 45 || s == 90)) || usage ;; p) p=${OPTARG} ;; *) ...
<pre><code>#!/bin/bash usage() { echo "Usage: $0 [-s &lt;45|90&gt;] [-p &lt;string&gt;]" 1&gt;&amp;2; exit 1; } while getopts ":s:p:" o; do case "${o}" in s) s=${OPTARG} ((s == 45 || s == 90)) || usage ;; p) p=${OPTARG} ;; *) ...
387, 390, 11225, 19020
bash, command-line-interface, getopts, shell
<h1>An example of how to use getopts in bash</h1> <p>I want to call <code>myscript</code> file in this way:</p> <pre><code>$ ./myscript -s 45 -p any_string </code></pre> <p>or</p> <pre><code>$ ./myscript -h #should display help $ ./myscript #should display help </code></pre> <p>My requirements are:</p> <ul> <li><c...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
212
bash
# An example of how to use getopts in bash I want to call `myscript` file in this way: ``` $ ./myscript -s 45 -p any_string ``` or ``` $ ./myscript -h #should display help $ ./myscript #should display help ``` My requirements are: - `getopts` here to get the input arguments - check that `-s` exists, if not r...
``` #!/bin/bash usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; } while getopts ":s:p:" o; do case "${o}" in s) s=${OPTARG} ((s == 45 || s == 90)) || usage ;; p) p=${OPTARG} ;; *) usage ...
2500436
How does "cat << EOF" work in bash?
1,082
2010-03-23 13:57:35
<p>I needed to write a script to enter multi-line input to a program (<code>psql</code>).</p> <p>After a bit of googling, I found the following syntax works:</p> <pre><code>cat &lt;&lt; EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; END; EOF </code></pre> <p>This correctly co...
1,842,146
35,364
2025-12-18 13:19:22
2,500,451
776
2010-03-23 13:58:41
224,671
2017-05-26 04:53:16
https://stackoverflow.com/q/2500436
https://stackoverflow.com/a/2500451
<p>This is called <strong><em>heredoc</strong> format</em> to provide a string into stdin. See <a href="https://en.wikipedia.org/wiki/Here_document#Unix_shells" rel="noreferrer">https://en.wikipedia.org/wiki/Here_document#Unix_shells</a> for more details.</p> <hr> <p>From <code>man bash</code>:</p> <blockquote> <h...
<p>This is called <strong><em>heredoc</strong> format</em> to provide a string into stdin. See <a href="https://en.wikipedia.org/wiki/Here_document#Unix_shells" rel="noreferrer">https://en.wikipedia.org/wiki/Here_document#Unix_shells</a> for more details.</p> <hr> <p>From <code>man bash</code>:</p> <blockquote> <h...
58, 387, 531, 7449
bash, heredoc, linux, scripting
<h1>How does "cat << EOF" work in bash?</h1> <p>I needed to write a script to enter multi-line input to a program (<code>psql</code>).</p> <p>After a bit of googling, I found the following syntax works:</p> <pre><code>cat &lt;&lt; EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
213
bash
# How does "cat << EOF" work in bash? I needed to write a script to enter multi-line input to a program (`psql`). After a bit of googling, I found the following syntax works: ``` cat << EOF | psql ---params BEGIN; `pg_dump ----something` update table .... statement ...; END; EOF ``` This correctly constructs the...
This is called ***heredoc*** format to provide a string into stdin. See <https://en.wikipedia.org/wiki/Here_document#Unix_shells> for more details. --- From `man bash`: > ## Here Documents > > This type of redirection instructs the shell to read input from > the current source until a line > containing only word (wi...
3371294
How can I recall the argument of the previous bash command?
443
2010-07-30 12:16:17
<p>Is there a way in Bash to recall the argument of the previous command?</p> <p>I usually do <code>vi file.c</code> followed by <code>gcc file.c</code>. </p> <p>Is there a way in Bash to recall the argument of the previous command?</p>
147,577
406,689
2024-07-05 13:02:22
3,371,299
772
2010-07-30 12:17:59
227,665
2022-11-18 17:30:58
https://stackoverflow.com/q/3371294
https://stackoverflow.com/a/3371299
<p>You can use <code>$_</code> or <code>!$</code> to recall the last argument of the previous command.</p> <p>Also <kbd>Alt</kbd> + <kbd>.</kbd> can be used to recall the last argument of any of the previous commands.</p>
<p>You can use <code>$_</code> or <code>!$</code> to recall the last argument of the previous command.</p> <p>Also <kbd>Alt</kbd> + <kbd>.</kbd> can be used to recall the last argument of any of the previous commands.</p>
34, 58, 387, 1796
bash, command, linux, unix
<h1>How can I recall the argument of the previous bash command?</h1> <p>Is there a way in Bash to recall the argument of the previous command?</p> <p>I usually do <code>vi file.c</code> followed by <code>gcc file.c</code>. </p> <p>Is there a way in Bash to recall the argument of the previous command?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
214
bash
# How can I recall the argument of the previous bash command? Is there a way in Bash to recall the argument of the previous command? I usually do `vi file.c` followed by `gcc file.c`. Is there a way in Bash to recall the argument of the previous command?
You can use `$_` or `!$` to recall the last argument of the previous command. Also `Alt` + `.` can be used to recall the last argument of any of the previous commands.
160924
How can I kill a process by name instead of PID, on Linux?
549
2008-10-02 04:57:20
<p>Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:</p> <pre><code>jeremy@jeremy-desktop:~$ ps aux | grep firefox jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/firefox-3.0.1/firefox jeremy 7578 0.0 0.3 3004 768 pts/0 S+ ...
895,285
813
2026-01-08 06:25:08
160,926
770
2008-10-02 04:58:24
9,611
2014-06-27 17:08:48
https://stackoverflow.com/q/160924
https://stackoverflow.com/a/160926
<pre><code>pkill firefox </code></pre> <p>More information: <a href="http://linux.about.com/library/cmd/blcmdl1_pkill.htm" rel="noreferrer">http://linux.about.com/library/cmd/blcmdl1_pkill.htm</a></p>
<pre><code>pkill firefox </code></pre> <p>More information: <a href="http://linux.about.com/library/cmd/blcmdl1_pkill.htm" rel="noreferrer">http://linux.about.com/library/cmd/blcmdl1_pkill.htm</a></p>
58, 387, 390, 5779
bash, kill, linux, shell
<h1>How can I kill a process by name instead of PID, on Linux?</h1> <p>Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:</p> <pre><code>jeremy@jeremy-desktop:~$ ps aux | grep firefox jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/fire...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
215
bash
# How can I kill a process by name instead of PID, on Linux? Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this: ``` jeremy@jeremy-desktop:~$ ps aux | grep firefox jeremy 7451 25.0 27.4 170536 65680 ? Sl 22:39 1:18 /usr/lib/firefox-3.0.1/firefox je...
``` pkill firefox ``` More information: <http://linux.about.com/library/cmd/blcmdl1_pkill.htm>