Dataset Viewer
Auto-converted to Parquet Duplicate
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
59895
How do I get the directory where a Bash script is located from within the script itself?
6,405
2008-09-12 20:39:56
<p>How do I get the path of the directory in which a <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> script is located, <em>inside</em> that script?</p> <p>I want to use a Bash script as a launcher for another application. I want to change the working directory to the one where th...
2,834,402
2,908
2025-06-10 04:21:43
246,128
8,291
2008-10-29 08:36:45
7,412
2025-05-22 03:57:59
https://stackoverflow.com/q/59895
https://stackoverflow.com/a/246128
<pre><code>#!/usr/bin/env bash SCRIPT_DIR=$( cd -- &quot;$( dirname -- &quot;${BASH_SOURCE[0]}&quot; )&quot; &amp;&gt; /dev/null &amp;&amp; pwd ) </code></pre> <p>is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.</p> <p>It will work as long as the ...
<pre><code>#!/usr/bin/env bash SCRIPT_DIR=$( cd -- &quot;$( dirname -- &quot;${BASH_SOURCE[0]}&quot; )&quot; &amp;&gt; /dev/null &amp;&amp; pwd ) </code></pre> <p>is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.</p> <p>It will work as long as the ...
218, 387
bash, directory
<h1>How do I get the directory where a Bash script is located from within the script itself?</h1> <p>How do I get the path of the directory in which a <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Bash</a> script is located, <em>inside</em> that script?</p> <p>I want to use a Bash script...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
16
bash
# How do I get the directory where a Bash script is located from within the script itself? How do I get the path of the directory in which a [Bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) script is located, *inside* that script? I want to use a Bash script as a launcher for another application. I want to ...
``` #!/usr/bin/env bash SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) ``` is a useful one-liner which will give you the full directory name of the script no matter where it is being called from. It will work as long as the last component of the path used to find the script is not a ...
11304895
How do I copy a folder from remote to local using scp?
3,597
2012-07-03 05:17:58
<p>How do I copy a folder from remote to local host using <code>scp</code>?</p> <p>I use <code>ssh</code> to log in to my server.<br /> Then, I would like to copy the remote folder <code>foo</code> to local <code>/home/user/Desktop</code>.</p> <p>How do I achieve this?</p>
3,900,240
1,469,282
2024-09-17 16:05:15
11,304,926
6,406
2012-07-03 05:21:39
774,116
2020-10-15 01:21:14
https://stackoverflow.com/q/11304895
https://stackoverflow.com/a/11304926
<pre><code>scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ </code></pre> <p>By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.</p> <p>From <code>man scp</code> (See <a href="http://man7.org/li...
<pre><code>scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ </code></pre> <p>By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.</p> <p>From <code>man scp</code> (See <a href="http://man7.org/li...
386, 390, 1231, 3149, 4330
command-line, copy, scp, shell, ssh
<h1>How do I copy a folder from remote to local using scp?</h1> <p>How do I copy a folder from remote to local host using <code>scp</code>?</p> <p>I use <code>ssh</code> to log in to my server.<br /> Then, I would like to copy the remote folder <code>foo</code> to local <code>/home/user/Desktop</code>.</p> <p>How do I ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
17
bash
# How do I copy a folder from remote to local using scp? How do I copy a folder from remote to local host using `scp`? I use `ssh` to log in to my server. Then, I would like to copy the remote folder `foo` to local `/home/user/Desktop`. How do I achieve this?
``` scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ ``` By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory. From `man scp` (See [online manual](http://man7.org/linux/man-pages/man1/scp.1.html...
59838
How do I check if a directory exists or not in a Bash shell script?
4,553
2008-09-12 20:06:25
<p>What command checks if a directory exists or not within a Bash shell script?</p>
3,495,129
4,120
2023-12-23 06:16:55
59,839
6,152
2008-09-12 20:07:06
4,120
2022-08-01 01:21:58
https://stackoverflow.com/q/59838
https://stackoverflow.com/a/59839
<p>To check if a directory exists:</p> <pre><code>if [ -d &quot;$DIRECTORY&quot; ]; then echo &quot;$DIRECTORY does exist.&quot; fi </code></pre> <p>To check if a directory does not exist:</p> <pre><code>if [ ! -d &quot;$DIRECTORY&quot; ]; then echo &quot;$DIRECTORY does not exist.&quot; fi </code></pre> <hr /> <p>...
<p>To check if a directory exists:</p> <pre><code>if [ -d &quot;$DIRECTORY&quot; ]; then echo &quot;$DIRECTORY does exist.&quot; fi </code></pre> <p>To check if a directory does not exist:</p> <pre><code>if [ ! -d &quot;$DIRECTORY&quot; ]; then echo &quot;$DIRECTORY does not exist.&quot; fi </code></pre> <hr /> <p>...
34, 387, 390, 1796, 1993
bash, command, posix, shell, unix
<h1>How do I check if a directory exists or not in a Bash shell script?</h1> <p>What command checks if a directory exists or not within a Bash shell script?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
18
bash
# How do I check if a directory exists or not in a Bash shell script? What command checks if a directory exists or not within a Bash shell script?
To check if a directory exists: ``` if [ -d "$DIRECTORY" ]; then echo "$DIRECTORY does exist." fi ``` To check if a directory does not exist: ``` if [ ! -d "$DIRECTORY" ]; then echo "$DIRECTORY does not exist." fi ``` --- However, as [Jon Ericson](https://stackoverflow.com/users/1438/jon-ericson) points out, s...
89228
How do I execute a program or call a system command?
6,282
2008-09-18 01:35:30
<p>How do I call an external command within Python as if I had typed it in a shell or command prompt?</p>
5,200,895
17,085
2025-11-04 02:52:45
89,243
5,961
2008-09-18 01:39:35
11,465
2023-05-19 23:52:54
https://stackoverflow.com/q/89228
https://stackoverflow.com/a/89243
<p>Use <a href="https://docs.python.org/library/subprocess.html#subprocess.run" rel="noreferrer"><code>subprocess.run</code></a>:</p> <pre class="lang-py prettyprint-override"><code>import subprocess subprocess.run([&quot;ls&quot;, &quot;-l&quot;]) </code></pre> <p>Another common way is <a href="https://docs.python.o...
<p>Use <a href="https://docs.python.org/library/subprocess.html#subprocess.run" rel="noreferrer"><code>subprocess.run</code></a>:</p> <pre class="lang-py prettyprint-override"><code>import subprocess subprocess.run([&quot;ls&quot;, &quot;-l&quot;]) </code></pre> <p>Another common way is <a href="https://docs.python.o...
16, 390, 391, 1796, 2348
command, python, shell, subprocess, terminal
<h1>How do I execute a program or call a system command?</h1> <p>How do I call an external command within Python as if I had typed it in a shell or command prompt?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
19
bash
# How do I execute a program or call a system command? How do I call an external command within Python as if I had typed it in a shell or command prompt?
Use [`subprocess.run`](https://docs.python.org/library/subprocess.html#subprocess.run): ``` import subprocess subprocess.run(["ls", "-l"]) ``` Another common way is [`os.system`](https://docs.python.org/library/os.html#os.system) but you shouldn't use it because it is unsafe if any parts of the command come from out...
638975
How do I tell if a file does not exist in Bash?
4,162
2009-03-12 14:48:43
<p>This checks if a file exists:</p> <pre><code>#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo &quot;File $FILE exists.&quot; else echo &quot;File $FILE does not exist.&quot; fi </code></pre> <p>How do I only check if the file does <strong>not</strong> exist?</p>
3,439,075
1,288
2024-06-18 10:32:34
638,980
5,614
2009-03-12 14:50:01
75,170
2022-07-17 00:26:03
https://stackoverflow.com/q/638975
https://stackoverflow.com/a/638980
<p>The <a href="http://man7.org/linux/man-pages/man1/test.1.html" rel="noreferrer"><code>test</code></a> command (written as <code>[</code> here) has a &quot;not&quot; logical operator, <code>!</code> (exclamation mark):</p> <pre><code>if [ ! -f /tmp/foo.txt ]; then echo &quot;File not found!&quot; fi </code></pre>...
<p>The <a href="http://man7.org/linux/man-pages/man1/test.1.html" rel="noreferrer"><code>test</code></a> command (written as <code>[</code> here) has a &quot;not&quot; logical operator, <code>!</code> (exclamation mark):</p> <pre><code>if [ ! -f /tmp/foo.txt ]; then echo &quot;File not found!&quot; fi </code></pre>...
387, 531, 724
bash, file-io, scripting
<h1>How do I tell if a file does not exist in Bash?</h1> <p>This checks if a file exists:</p> <pre><code>#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo &quot;File $FILE exists.&quot; else echo &quot;File $FILE does not exist.&quot; fi </code></pre> <p>How do I only check if the file does <strong>not</strong...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
20
bash
# How do I tell if a file does not exist in Bash? This checks if a file exists: ``` #!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi ``` How do I only check if the file does **not** exist?
The [`test`](http://man7.org/linux/man-pages/man1/test.1.html) command (written as `[` here) has a "not" logical operator, `!` (exclamation mark): ``` if [ ! -f /tmp/foo.txt ]; then echo "File not found!" fi ```
229551
How to check if a string contains a substring in Bash
3,658
2008-10-23 12:37:31
<p>I have a string in Bash:</p> <pre class="lang-sh prettyprint-override"><code>string=&quot;My string&quot; </code></pre> <p>How can I test if it contains another string?</p> <pre class="lang-sh prettyprint-override"><code>if [ $string ?? 'foo' ]; then echo &quot;It's there!&quot; fi </code></pre> <p>Where <code>??<...
3,391,086
30,773
2026-01-08 11:58:40
229,606
5,028
2008-10-23 12:55:24
21,632
2020-04-17 01:20:42
https://stackoverflow.com/q/229551
https://stackoverflow.com/a/229606
<p>You can use <a href="https://stackoverflow.com/a/229585/3755692">Marcus's answer (* wildcards)</a> outside a case statement, too, if you use double brackets:</p> <pre><code>string='My long string' if [[ $string == *"My long"* ]]; then echo "It's there!" fi </code></pre> <p>Note that spaces in the needle string n...
<p>You can use <a href="https://stackoverflow.com/a/229585/3755692">Marcus's answer (* wildcards)</a> outside a case statement, too, if you use double brackets:</p> <pre><code>string='My long string' if [[ $string == *"My long"* ]]; then echo "It's there!" fi </code></pre> <p>Note that spaces in the needle string n...
139, 387, 390, 4371, 10327
bash, sh, shell, string, substring
<h1>How to check if a string contains a substring in Bash</h1> <p>I have a string in Bash:</p> <pre class="lang-sh prettyprint-override"><code>string=&quot;My string&quot; </code></pre> <p>How can I test if it contains another string?</p> <pre class="lang-sh prettyprint-override"><code>if [ $string ?? 'foo' ]; then e...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
21
bash
# How to check if a string contains a substring in Bash I have a string in Bash: ``` string="My string" ``` How can I test if it contains another string? ``` if [ $string ?? 'foo' ]; then echo "It's there!" fi ``` Where `??` is my unknown operator. Do I use `echo` and `grep`? ``` if echo "$string" | grep 'foo';...
You can use [Marcus's answer (* wildcards)](https://stackoverflow.com/a/229585/3755692) outside a case statement, too, if you use double brackets: ``` string='My long string' if [[ $string == *"My long"* ]]; then echo "It's there!" fi ``` Note that spaces in the needle string need to be placed between double quotes...
4181703
How to concatenate string variables in Bash
3,611
2010-11-15 05:38:35
<p>In PHP, strings are concatenated together as follows:</p> <pre><code>$foo = &quot;Hello&quot;; $foo .= &quot; World&quot;; </code></pre> <p>Here, <code>$foo</code> becomes <code>&quot;Hello World&quot;</code>.</p> <p>How is this accomplished in Bash?</p>
5,351,453
170,365
2025-10-16 09:01:13
4,181,721
4,890
2010-11-15 05:41:36
227,665
2019-09-01 12:51:28
https://stackoverflow.com/q/4181703
https://stackoverflow.com/a/4181721
<pre><code>foo="Hello" foo="${foo} World" echo "${foo}" &gt; Hello World </code></pre> <p>In general to concatenate two variables you can just write them one after another:</p> <pre><code>a='Hello' b='World' c="${a} ${b}" echo "${c}" &gt; Hello World </code></pre>
<pre><code>foo="Hello" foo="${foo} World" echo "${foo}" &gt; Hello World </code></pre> <p>In general to concatenate two variables you can just write them one after another:</p> <pre><code>a='Hello' b='World' c="${a} ${b}" echo "${c}" &gt; Hello World </code></pre>
367, 387, 390, 6050, 7792
bash, concatenation, shell, string-concatenation, syntax
<h1>How to concatenate string variables in Bash</h1> <p>In PHP, strings are concatenated together as follows:</p> <pre><code>$foo = &quot;Hello&quot;; $foo .= &quot; World&quot;; </code></pre> <p>Here, <code>$foo</code> becomes <code>&quot;Hello World&quot;</code>.</p> <p>How is this accomplished in Bash?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
22
bash
# How to concatenate string variables in Bash In PHP, strings are concatenated together as follows: ``` $foo = "Hello"; $foo .= " World"; ``` Here, `$foo` becomes `"Hello World"`. How is this accomplished in Bash?
``` foo="Hello" foo="${foo} World" echo "${foo}" > Hello World ``` In general to concatenate two variables you can just write them one after another: ``` a='Hello' b='World' c="${a} ${b}" echo "${c}" > Hello World ```
793858
How to mkdir only if a directory does not already exist?
2,998
2009-04-27 14:47:44
<p>I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the <code>mkdir</code> command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exis...
2,292,191
73,371
2023-03-17 09:06:20
793,867
4,852
2009-04-27 14:49:46
69,755
2019-01-23 21:04:22
https://stackoverflow.com/q/793858
https://stackoverflow.com/a/793867
<p>Try <a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html" rel="noreferrer"><code>mkdir -p</code></a>:</p> <pre><code>mkdir -p foo </code></pre> <p>Note that this will also create any intermediate directories that don't exist; for instance,</p> <pre><code>mkdir -p foo/bar/baz </code></pre> ...
<p>Try <a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html" rel="noreferrer"><code>mkdir -p</code></a>:</p> <pre><code>mkdir -p foo </code></pre> <p>Note that this will also create any intermediate directories that don't exist; for instance,</p> <pre><code>mkdir -p foo/bar/baz </code></pre> ...
390, 531, 989, 1964, 24423
aix, ksh, mkdir, scripting, shell
<h1>How to mkdir only if a directory does not already exist?</h1> <p>I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the <code>mkdir</code> command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
23
bash
# How to mkdir only if a directory does not already exist? I am writing a shell script to run under the KornShell (ksh) on AIX. I would like to use the `mkdir` command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the direct...
Try [`mkdir -p`](http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html): ``` mkdir -p foo ``` Note that this will also create any intermediate directories that don't exist; for instance, ``` mkdir -p foo/bar/baz ``` will create directories `foo`, `foo/bar`, and `foo/bar/baz` if they don't exist. Some...
592620
How can I check if a program exists from a Bash script?
3,215
2009-02-26 21:52:49
<p>How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?</p> <p>It seems like it should be easy, but it's been stumping me.</p>
1,298,786
2,687
2025-06-01 11:10:42
677,212
4,596
2009-03-24 12:45:20
58,803
2025-06-01 11:10:42
https://stackoverflow.com/q/592620
https://stackoverflow.com/a/677212
<h2>Answer</h2> <p>POSIX compatible:</p> <pre><code>command -v &lt;the_command&gt; </code></pre> <p>Example use:</p> <pre><code>if ! command -v &lt;the_command&gt; &gt;/dev/null 2&gt;&amp;1 then echo &quot;&lt;the_command&gt; could not be found&quot; exit 1 fi </code></pre> <p>For Bash specific environments:</p...
<h2>Answer</h2> <p>POSIX compatible:</p> <pre><code>command -v &lt;the_command&gt; </code></pre> <p>Example use:</p> <pre><code>if ! command -v &lt;the_command&gt; &gt;/dev/null 2&gt;&amp;1 then echo &quot;&lt;the_command&gt; could not be found&quot; exit 1 fi </code></pre> <p>For Bash specific environments:</p...
387
bash
<h1>How can I check if a program exists from a Bash script?</h1> <p>How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?</p> <p>It seems like it should be easy, but it's been stumping me.</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
24
bash
# How can I check if a program exists from a Bash script? How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script? It seems like it should be easy, but it's been stumping me.
## Answer POSIX compatible: ``` command -v <the_command> ``` Example use: ``` if ! command -v <the_command> >/dev/null 2>&1 then echo "<the_command> could not be found" exit 1 fi ``` For Bash specific environments: ``` hash <the_command> # For regular commands. Or... type <the_command> # To check built-in...
965053
Extract filename and extension in Bash
2,918
2009-06-08 14:00:29
<p>I want to get the filename (without extension) and the extension separately.</p> <p>The best solution I found so far is:</p> <pre><code>NAME=`echo &quot;$FILE&quot; | cut -d'.' -f1` EXTENSION=`echo &quot;$FILE&quot; | cut -d'.' -f2` </code></pre> <p>This is wrong because it doesn't work if the file name contains mul...
2,427,384
5,475
2025-08-17 16:24:24
965,072
4,538
2009-06-08 14:05:19
17,833
2025-03-21 07:22:40
https://stackoverflow.com/q/965053
https://stackoverflow.com/a/965072
<p>First, get file name without the path:</p> <pre class="lang-bash prettyprint-override"><code>filename=$(basename -- &quot;$fullfile&quot;) extension=&quot;${filename##*.}&quot; filename=&quot;${filename%.*}&quot; </code></pre> <p>Alternatively, you can focus on the last '/' of the path instead of the '.' which shoul...
<p>First, get file name without the path:</p> <pre class="lang-bash prettyprint-override"><code>filename=$(basename -- &quot;$fullfile&quot;) extension=&quot;${filename##*.}&quot; filename=&quot;${filename%.*}&quot; </code></pre> <p>Alternatively, you can focus on the last '/' of the path instead of the '.' which shoul...
139, 387, 1062
bash, filenames, string
<h1>Extract filename and extension in Bash</h1> <p>I want to get the filename (without extension) and the extension separately.</p> <p>The best solution I found so far is:</p> <pre><code>NAME=`echo &quot;$FILE&quot; | cut -d'.' -f1` EXTENSION=`echo &quot;$FILE&quot; | cut -d'.' -f2` </code></pre> <p>This is wrong becau...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
25
bash
# Extract filename and extension in Bash I want to get the filename (without extension) and the extension separately. The best solution I found so far is: ``` NAME=`echo "$FILE" | cut -d'.' -f1` EXTENSION=`echo "$FILE" | cut -d'.' -f2` ``` This is wrong because it doesn't work if the file name contains multiple `.`...
First, get file name without the path: ``` filename=$(basename -- "$fullfile") extension="${filename##*.}" filename="${filename%.*}" ``` Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions: ``` filename="${fullfile##*/}" ``` Yo...
5905054
How can I recursively find all files in current and subfolders based on wildcard matching?
3,144
2011-05-05 23:01:34
<p>How can I recursively find all files in current and subfolders based on wildcard matching?</p>
5,129,961
302,064
2023-12-29 09:16:26
5,905,066
4,478
2011-05-05 23:03:37
82,219
2023-12-29 09:16:26
https://stackoverflow.com/q/5905054
https://stackoverflow.com/a/5905066
<p>Use <a href="http://linux.die.net/man/1/find" rel="noreferrer"><code>find</code></a>:</p> <pre><code>find . -name &quot;foo*&quot; </code></pre> <p><code>find</code> needs a starting point, so the <code>.</code> (dot) points to the current directory.</p> <p>If you need <strong>case insensitive</strong> search use :<...
<p>Use <a href="http://linux.die.net/man/1/find" rel="noreferrer"><code>find</code></a>:</p> <pre><code>find . -name &quot;foo*&quot; </code></pre> <p><code>find</code> needs a starting point, so the <code>.</code> (dot) points to the current directory.</p> <p>If you need <strong>case insensitive</strong> search use :<...
58, 390
linux, shell
<h1>How can I recursively find all files in current and subfolders based on wildcard matching?</h1> <p>How can I recursively find all files in current and subfolders based on wildcard matching?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
26
bash
# How can I recursively find all files in current and subfolders based on wildcard matching? How can I recursively find all files in current and subfolders based on wildcard matching?
Use [`find`](http://linux.die.net/man/1/find): ``` find . -name "foo*" ``` `find` needs a starting point, so the `.` (dot) points to the current directory. If you need **case insensitive** search use : ``` find . -iname "foo*" ```
8467424
Echo newline in Bash prints literal \n
3,667
2011-12-11 21:01:54
<p>How do I print a newline? This merely prints <code>\n</code>:</p> <pre><code>echo -e &quot;Hello,\nWorld!&quot; </code></pre> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>Hello,\nWorld! </code></pre>
3,688,424
187,644
2025-12-09 22:55:19
8,467,449
4,311
2011-12-11 21:04:56
56,338
2025-07-16 17:42:38
https://stackoverflow.com/q/8467424
https://stackoverflow.com/a/8467449
<p>Use <code>printf</code> instead:</p> <pre><code>printf 'Hello, \nWorld!\n' </code></pre> <p><code>printf</code> behaves more consistently across different environments than <code>echo</code>.</p>
<p>Use <code>printf</code> instead:</p> <pre><code>printf 'Hello, \nWorld!\n' </code></pre> <p><code>printf</code> behaves more consistently across different environments than <code>echo</code>.</p>
387, 3705, 13824
bash, echo, newline
<h1>Echo newline in Bash prints literal \n</h1> <p>How do I print a newline? This merely prints <code>\n</code>:</p> <pre><code>echo -e &quot;Hello,\nWorld!&quot; </code></pre> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>Hello,\nWorld! </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
27
bash
# Echo newline in Bash prints literal \n How do I print a newline? This merely prints `\n`: ``` echo -e "Hello,\nWorld!" ``` Output: ``` Hello,\nWorld! ```
Use `printf` instead: ``` printf 'Hello, \nWorld!\n' ``` `printf` behaves more consistently across different environments than `echo`.
1825585
Determine installed PowerShell version
2,915
2009-12-01 11:30:03
<p>How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?</p>
3,250,810
35,483
2025-11-15 13:22:28
1,825,807
3,878
2009-12-01 12:12:38
null
2020-01-02 07:33:10
https://stackoverflow.com/q/1825585
https://stackoverflow.com/a/1825807
<p>Use <code>$PSVersionTable.PSVersion</code> to determine the engine version. If the variable does not exist, it is safe to assume the engine is version <code>1.0</code>.</p> <p>Note that <code>$Host.Version</code> and <code>(Get-Host).Version</code> are not reliable - they reflect the version of the host only, not t...
<p>Use <code>$PSVersionTable.PSVersion</code> to determine the engine version. If the variable does not exist, it is safe to assume the engine is version <code>1.0</code>.</p> <p>Note that <code>$Host.Version</code> and <code>(Get-Host).Version</code> are not reliable - they reflect the version of the host only, not t...
526, 5792
powershell, version
<h1>Determine installed PowerShell version</h1> <p>How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
28
bash
# Determine installed PowerShell version How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?
Use `$PSVersionTable.PSVersion` to determine the engine version. If the variable does not exist, it is safe to assume the engine is version `1.0`. Note that `$Host.Version` and `(Get-Host).Version` are not reliable - they reflect the version of the host only, not the engine. PowerGUI, PowerShellPLUS, etc. are all host...
192249
How do I parse command line arguments in Bash?
2,627
2008-10-10 16:57:19
<p>Say, I have a script that gets called with this line:</p> <pre><code>./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile </code></pre> <p>or this one:</p> <pre><code>./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile </code></pre> <p>What's the accepted way of parsing this such that in each case (o...
2,195,200
1,512
2025-05-30 12:27:53
14,203,146
3,687
2013-01-07 20:01:05
117,471
2024-03-18 06:12:10
https://stackoverflow.com/q/192249
https://stackoverflow.com/a/14203146
<h4>Bash Space-Separated (e.g., <code>--option argument</code>)</h4> <pre class="lang-bash prettyprint-override"><code>cat &gt;/tmp/demo-space-separated.sh &lt;&lt;'EOF' #!/bin/bash POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -e|--extension) EXTENSION=&quot;$2&quot; shift # past argument...
<h4>Bash Space-Separated (e.g., <code>--option argument</code>)</h4> <pre class="lang-bash prettyprint-override"><code>cat &gt;/tmp/demo-space-separated.sh &lt;&lt;'EOF' #!/bin/bash POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -e|--extension) EXTENSION=&quot;$2&quot; shift # past argument...
387, 531, 1231, 2313, 19020
arguments, bash, command-line, getopts, scripting
<h1>How do I parse command line arguments in Bash?</h1> <p>Say, I have a script that gets called with this line:</p> <pre><code>./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile </code></pre> <p>or this one:</p> <pre><code>./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile </code></pre> <p>What's th...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
29
bash
# How do I parse command line arguments in Bash? Say, I have a script that gets called with this line: ``` ./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile ``` or this one: ``` ./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile ``` What's the accepted way of parsing this such that in each case ...
#### Bash Space-Separated (e.g., `--option argument`) ``` cat >/tmp/demo-space-separated.sh <<'EOF' #!/bin/bash POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case $1 in -e|--extension) EXTENSION="$2" shift # past argument shift # past value ;; -s|--searchpath) SEARCHPATH="$2" ...
818255
What does " 2>&1 " mean?
3,260
2009-05-03 22:57:00
<p>To combine <code>stderr</code> and <code>stdout</code> into the <code>stdout</code> stream, we append this to a command:</p> <pre><code>2&gt;&amp;1 </code></pre> <p>For example, the following command shows the first few errors from compiling <code>main.cpp</code>:</p> <pre><code>g++ main.cpp 2&gt;&amp;1 | head </cod...
1,963,820
30,529
2024-11-07 14:50:17
818,284
3,676
2009-05-03 23:04:53
40,005
2022-07-17 06:18:24
https://stackoverflow.com/q/818255
https://stackoverflow.com/a/818284
<p>File descriptor 1 is the standard output (<code>stdout</code>).<br> File descriptor 2 is the standard error (<code>stderr</code>).</p> <p>At first, <code>2&gt;1</code> may look like a good way to redirect <code>stderr</code> to <code>stdout</code>. However, it will actually be interpreted as &quot;redirect <code>std...
<p>File descriptor 1 is the standard output (<code>stdout</code>).<br> File descriptor 2 is the standard error (<code>stderr</code>).</p> <p>At first, <code>2&gt;1</code> may look like a good way to redirect <code>stderr</code> to <code>stdout</code>. However, it will actually be interpreted as &quot;redirect <code>std...
34, 387, 390
bash, shell, unix
<h1>What does " 2>&1 " mean?</h1> <p>To combine <code>stderr</code> and <code>stdout</code> into the <code>stdout</code> stream, we append this to a command:</p> <pre><code>2&gt;&amp;1 </code></pre> <p>For example, the following command shows the first few errors from compiling <code>main.cpp</code>:</p> <pre><code>g++...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
30
bash
# What does " 2>&1 " mean? To combine `stderr` and `stdout` into the `stdout` stream, we append this to a command: ``` 2>&1 ``` For example, the following command shows the first few errors from compiling `main.cpp`: ``` g++ main.cpp 2>&1 | head ``` But what does `2>&1` mean?
File descriptor 1 is the standard output (`stdout`). File descriptor 2 is the standard error (`stderr`). At first, `2>1` may look like a good way to redirect `stderr` to `stdout`. However, it will actually be interpreted as "redirect `stderr` to a file named `1`". `&` indicates that what follows and precedes is a *...
4037939
PowerShell says "execution of scripts is disabled on this system."
3,013
2010-10-27 21:39:29
<p>I am trying to run a <code>cmd</code> file that calls a PowerShell script from <code>cmd.exe</code>, but I am getting this error:</p> <blockquote> <p><code>Management_Install.ps1</code> cannot be loaded because the execution of scripts is disabled on this system.</p> </blockquote> <p>I ran this command:</p> <pre cla...
5,280,444
489,400
2025-08-24 19:14:52
4,038,991
3,670
2010-10-28 01:16:25
135,965
2022-02-24 18:26:00
https://stackoverflow.com/q/4037939
https://stackoverflow.com/a/4038991
<p>If you're using <a href="https://en.wikipedia.org/wiki/Windows_Server_2008" rel="noreferrer">Windows Server 2008</a> R2 then there is an <em>x64</em> and <em>x86</em> version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?</p> <p>As an <em>Admini...
<p>If you're using <a href="https://en.wikipedia.org/wiki/Windows_Server_2008" rel="noreferrer">Windows Server 2008</a> R2 then there is an <em>x64</em> and <em>x86</em> version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?</p> <p>As an <em>Admini...
526, 36466
powershell, windows-server-2008-r2
<h1>PowerShell says "execution of scripts is disabled on this system."</h1> <p>I am trying to run a <code>cmd</code> file that calls a PowerShell script from <code>cmd.exe</code>, but I am getting this error:</p> <blockquote> <p><code>Management_Install.ps1</code> cannot be loaded because the execution of scripts is di...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
31
bash
# PowerShell says "execution of scripts is disabled on this system." I am trying to run a `cmd` file that calls a PowerShell script from `cmd.exe`, but I am getting this error: > `Management_Install.ps1` cannot be loaded because the execution of scripts is disabled on this system. I ran this command: ``` Set-Execut...
If you're using [Windows Server 2008](https://en.wikipedia.org/wiki/Windows_Server_2008) R2 then there is an *x64* and *x86* version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts? As an *Administrator*, you can set the execution policy by typing t...
5410757
How can I delete all lines that contain a specific string from a text file?
2,416
2011-03-23 19:46:07
<p>How would I use sed to delete all lines in a text file that contain a specific string?</p>
2,684,037
667,236
2025-12-13 14:13:19
5,410,784
3,649
2011-03-23 19:48:46
207,248
2025-12-12 05:23:22
https://stackoverflow.com/q/5410757
https://stackoverflow.com/a/5410784
<p>To remove the line and print the output to standard out:</p> <pre><code>sed '/pattern to match/d' ./infile </code></pre> <p>To directly modify the file (it does not work with BSD sed):</p> <pre><code>sed -i '/pattern to match/d' ./infile </code></pre> <p>The same, but for BSD sed (Mac OS X and FreeBSD. It does not w...
<p>To remove the line and print the output to standard out:</p> <pre><code>sed '/pattern to match/d' ./infile </code></pre> <p>To directly modify the file (it does not work with BSD sed):</p> <pre><code>sed -i '/pattern to match/d' ./infile </code></pre> <p>The same, but for BSD sed (Mac OS X and FreeBSD. It does not w...
390, 5282, 12189, 13401
in-place, sed, shell, text-parsing
<h1>How can I delete all lines that contain a specific string from a text file?</h1> <p>How would I use sed to delete all lines in a text file that contain a specific string?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
32
bash
# How can I delete all lines that contain a specific string from a text file? How would I use sed to delete all lines in a text file that contain a specific string?
To remove the line and print the output to standard out: ``` sed '/pattern to match/d' ./infile ``` To directly modify the file (it does not work with BSD sed): ``` sed -i '/pattern to match/d' ./infile ``` The same, but for BSD sed (Mac OS X and FreeBSD. It does not work with GNU sed): ``` sed -i '' '/pattern to ...
8880603
Loop through an array of strings in Bash?
2,420
2012-01-16 13:21:16
<p>I want to write a script that loops through 15 strings (array possibly?) Is that possible?</p> <pre><code>for databaseName in listOfNames then # Do something end </code></pre>
2,293,789
218,183
2025-11-19 13:31:32
8,880,633
3,621
2012-01-16 13:24:47
548,225
2025-11-19 13:31:32
https://stackoverflow.com/q/8880603
https://stackoverflow.com/a/8880633
<pre><code>## declare an array variable declare -a arr=(&quot;element 1&quot; &quot;element 2&quot; &quot;element 3&quot;) ## loop through above array (quotes are important if your elements may contain spaces) for i in &quot;${arr[@]}&quot; do echo &quot;$i&quot; # or do whatever with individual element of array...
<pre><code>## declare an array variable declare -a arr=(&quot;element 1&quot; &quot;element 2&quot; &quot;element 3&quot;) ## loop through above array (quotes are important if your elements may contain spaces) for i in &quot;${arr[@]}&quot; do echo &quot;$i&quot; # or do whatever with individual element of array...
114, 387, 390
arrays, bash, shell
<h1>Loop through an array of strings in Bash?</h1> <p>I want to write a script that loops through 15 strings (array possibly?) Is that possible?</p> <pre><code>for databaseName in listOfNames then # Do something end </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
33
bash
# Loop through an array of strings in Bash? I want to write a script that loops through 15 strings (array possibly?) Is that possible? ``` for databaseName in listOfNames then # Do something end ```
``` ## declare an array variable declare -a arr=("element 1" "element 2" "element 3") ## loop through above array (quotes are important if your elements may contain spaces) for i in "${arr[@]}" do echo "$i" # or do whatever with individual element of array done # You can access them using echo "${arr[0]}", "${a...
2518127
How to reload .bashrc settings without logging out and back in again?
2,268
2010-03-25 17:58:36
<p>If I make changes to <code>.bashrc</code>, how do I reload it without logging out and back in?</p>
1,888,242
292,553
2024-09-06 14:44:36
2,518,150
3,583
2010-03-25 18:01:04
245,602
2020-10-21 14:18:32
https://stackoverflow.com/q/2518127
https://stackoverflow.com/a/2518150
<p>You can enter the long form command:</p> <pre><code>source ~/.bashrc </code></pre> <p>or you can use the shorter version of the command:</p> <pre><code>. ~/.bashrc </code></pre>
<p>You can enter the long form command:</p> <pre><code>source ~/.bashrc </code></pre> <p>or you can use the shorter version of the command:</p> <pre><code>. ~/.bashrc </code></pre>
387, 390, 391, 403, 12126
bash, profile, reload, shell, terminal
<h1>How to reload .bashrc settings without logging out and back in again?</h1> <p>If I make changes to <code>.bashrc</code>, how do I reload it without logging out and back in?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
34
bash
# How to reload .bashrc settings without logging out and back in again? If I make changes to `.bashrc`, how do I reload it without logging out and back in?
You can enter the long form command: ``` source ~/.bashrc ``` or you can use the shorter version of the command: ``` . ~/.bashrc ```
3601515
How to check if a variable is set in Bash
2,518
2010-08-30 14:54:38
<p>How do I know if a variable is set in Bash?</p> <p>For example, how do I check if the user gave the first parameter to a function?</p> <pre><code>function a { # if $1 is set ? } </code></pre>
2,489,781
260,127
2024-01-11 07:53:29
13,864,829
3,468
2012-12-13 17:04:53
1,633,643
2021-12-06 18:12:01
https://stackoverflow.com/q/3601515
https://stackoverflow.com/a/13864829
<h2>(Usually) The right way</h2> <pre><code>if [ -z ${var+x} ]; then echo &quot;var is unset&quot;; else echo &quot;var is set to '$var'&quot;; fi </code></pre> <p>where <code>${var+x}</code> is a <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02" rel="noreferrer">parameter ...
<h2>(Usually) The right way</h2> <pre><code>if [ -z ${var+x} ]; then echo &quot;var is unset&quot;; else echo &quot;var is set to '$var'&quot;; fi </code></pre> <p>where <code>${var+x}</code> is a <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02" rel="noreferrer">parameter ...
276, 387, 390
bash, shell, variables
<h1>How to check if a variable is set in Bash</h1> <p>How do I know if a variable is set in Bash?</p> <p>For example, how do I check if the user gave the first parameter to a function?</p> <pre><code>function a { # if $1 is set ? } </code></pre>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
35
bash
# How to check if a variable is set in Bash How do I know if a variable is set in Bash? For example, how do I check if the user gave the first parameter to a function? ``` function a { # if $1 is set ? } ```
## (Usually) The right way ``` if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi ``` where `${var+x}` is a [parameter expansion](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02) which evaluates to nothing if `var` is unset, and substitutes the str...
6482377
Check existence of input argument in a Bash shell script
2,049
2011-06-26 05:49:21
<p>I need to check the existence of an input argument. I have the following script</p> <pre><code>if [ "$1" -gt "-1" ] then echo hi fi </code></pre> <p>I get</p> <pre><code>[: : integer expression expected </code></pre> <p>How do I check the input argument1 first to see if it exists?</p>
2,126,764
775,187
2024-06-03 04:03:01
6,482,403
3,395
2011-06-26 05:55:41
702,361
2021-01-28 08:38:46
https://stackoverflow.com/q/6482377
https://stackoverflow.com/a/6482403
<p>It is:</p> <pre class="lang-bash prettyprint-override"><code>if [ $# -eq 0 ] then echo &quot;No arguments supplied&quot; fi </code></pre> <p>The <code>$#</code> variable will tell you the number of input arguments the script was passed.</p> <p>Or you can check if an argument is an empty string or not like:</p>...
<p>It is:</p> <pre class="lang-bash prettyprint-override"><code>if [ $# -eq 0 ] then echo &quot;No arguments supplied&quot; fi </code></pre> <p>The <code>$#</code> variable will tell you the number of input arguments the script was passed.</p> <p>Or you can check if an argument is an empty string or not like:</p>...
387, 390
bash, shell
<h1>Check existence of input argument in a Bash shell script</h1> <p>I need to check the existence of an input argument. I have the following script</p> <pre><code>if [ "$1" -gt "-1" ] then echo hi fi </code></pre> <p>I get</p> <pre><code>[: : integer expression expected </code></pre> <p>How do I check the input ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
36
bash
# Check existence of input argument in a Bash shell script I need to check the existence of an input argument. I have the following script ``` if [ "$1" -gt "-1" ] then echo hi fi ``` I get ``` [: : integer expression expected ``` How do I check the input argument1 first to see if it exists?
It is: ``` if [ $# -eq 0 ] then echo "No arguments supplied" fi ``` The `$#` variable will tell you the number of input arguments the script was passed. Or you can check if an argument is an empty string or not like: ``` if [ -z "$1" ] then echo "No argument supplied" fi ``` The `-z` switch will test i...
1358540
How can I count all the lines of code in a directory recursively?
2,114
2009-08-31 17:42:20
<p>We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories.</p> <p>We don't need to ignore comments, as we're just trying to get a rough idea.</p> <pre><code>wc -l *.php </code></pre> <p>That command works great for a given directory, but it ignores subdire...
1,285,868
77,413
2024-08-01 03:57:59
1,358,573
3,342
2009-08-31 17:50:12
76,794
2021-05-27 09:50:00
https://stackoverflow.com/q/1358540
https://stackoverflow.com/a/1358573
<p><strong>Try:</strong></p> <pre><code>find . -name '*.php' | xargs wc -l </code></pre> <p>or (when file names include special characters such as spaces)</p> <pre><code>find . -name '*.php' | sed 's/.*/&quot;&amp;&quot;/' | xargs wc -l </code></pre> <p><strong><a href="https://dwheeler.com/sloccount/" rel="noreferrer...
<p><strong>Try:</strong></p> <pre><code>find . -name '*.php' | xargs wc -l </code></pre> <p>or (when file names include special characters such as spaces)</p> <pre><code>find . -name '*.php' | sed 's/.*/&quot;&amp;&quot;/' | xargs wc -l </code></pre> <p><strong><a href="https://dwheeler.com/sloccount/" rel="noreferrer...
387, 390
bash, shell
<h1>How can I count all the lines of code in a directory recursively?</h1> <p>We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories.</p> <p>We don't need to ignore comments, as we're just trying to get a rough idea.</p> <pre><code>wc -l *.php </code></pre...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
37
bash
# How can I count all the lines of code in a directory recursively? We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories. We don't need to ignore comments, as we're just trying to get a rough idea. ``` wc -l *.php ``` That command works great for a gi...
**Try:** ``` find . -name '*.php' | xargs wc -l ``` or (when file names include special characters such as spaces) ``` find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l ``` **[The SLOCCount tool](https://dwheeler.com/sloccount/)** may help as well. It will give an accurate source lines of code count for whatev...
4651437
How do I set a variable to the output of a command in Bash?
2,507
2011-01-10 20:58:02
<p>I have a pretty simple script that is something like the following:</p> <pre><code>#!/bin/bash VAR1="$1" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF </code></pre> <p>When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I...
3,105,029
570,402
2025-09-19 06:29:46
4,651,495
3,283
2011-01-10 21:04:18
8,454
2024-08-26 16:01:57
https://stackoverflow.com/q/4651437
https://stackoverflow.com/a/4651495
<p>In addition to backticks <code>`command`</code>, <a href="http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution" rel="noreferrer">command substitution</a> can be done with <code>$(command)</code> or <code>&quot;$(command)&quot;</code>, which I find easier to read, and allows for nesting.</p> <pre...
<p>In addition to backticks <code>`command`</code>, <a href="http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution" rel="noreferrer">command substitution</a> can be done with <code>$(command)</code> or <code>&quot;$(command)&quot;</code>, which I find easier to read, and allows for nesting.</p> <pre...
387, 390, 1231
bash, command-line, shell
<h1>How do I set a variable to the output of a command in Bash?</h1> <p>I have a pretty simple script that is something like the following:</p> <pre><code>#!/bin/bash VAR1="$1" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF </code></pre> <p>When I run this script from the command line and...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
38
bash
# How do I set a variable to the output of a command in Bash? I have a pretty simple script that is something like the following: ``` #!/bin/bash VAR1="$1" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF ``` When I run this script from the command line and pass it the arguments, I am not ...
In addition to backticks `` `command` ``, [command substitution](http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution) can be done with `$(command)` or `"$(command)"`, which I find easier to read, and allows for nesting. ``` OUTPUT="$(ls -1)" echo "${OUTPUT}" MULTILINE="$(ls \ -1)" echo "${MUL...
2264428
How to convert a string to lower case in Bash
1,893
2010-02-15 07:02:56
<p>Is there a way in <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" rel="tag">bash</a> to convert a string into a lower case string?</p> <p>For example, if I have:</p> <pre><code>a="Hi all" </code></pre> <p>I want to convert it to:</p> <pre><code>"hi all" </code></pre...
1,602,361
266,008
2024-10-02 06:03:31
2,264,537
3,129
2010-02-15 07:43:20
131,527
2024-10-02 06:03:31
https://stackoverflow.com/q/2264428
https://stackoverflow.com/a/2264537
<p>There are various ways:</p> <h3><a href="https://wikipedia.org/wiki/POSIX" rel="noreferrer">POSIX standard</a></h3> <h4><a href="https://wikipedia.org/wiki/Tr_%28Unix%29" rel="noreferrer">tr</a></h4> <pre><code>$ echo &quot;$a&quot; | tr '[:upper:]' '[:lower:]' hi all </code></pre> <h4><a href="https://wikipedia.org...
<p>There are various ways:</p> <h3><a href="https://wikipedia.org/wiki/POSIX" rel="noreferrer">POSIX standard</a></h3> <h4><a href="https://wikipedia.org/wiki/Tr_%28Unix%29" rel="noreferrer">tr</a></h4> <pre><code>$ echo &quot;$a&quot; | tr '[:upper:]' '[:lower:]' hi all </code></pre> <h4><a href="https://wikipedia.org...
139, 387, 390, 9848
bash, lowercase, shell, string
<h1>How to convert a string to lower case in Bash</h1> <p>Is there a way in <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged &#39;bash&#39;" rel="tag">bash</a> to convert a string into a lower case string?</p> <p>For example, if I have:</p> <pre><code>a="Hi all" </code></pre> <p>I want ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
39
bash
# How to convert a string to lower case in Bash Is there a way in [bash](/questions/tagged/bash "show questions tagged 'bash'") to convert a string into a lower case string? For example, if I have: ``` a="Hi all" ``` I want to convert it to: ``` "hi all" ```
There are various ways: ### [POSIX standard](https://wikipedia.org/wiki/POSIX) #### [tr](https://wikipedia.org/wiki/Tr_%28Unix%29) ``` $ echo "$a" | tr '[:upper:]' '[:lower:]' hi all ``` #### [AWK](https://wikipedia.org/wiki/AWK) ``` $ echo "$a" | awk '{print tolower($0)}' hi all ``` ## Non-POSIX You may run int...
1521462
Looping through the content of a file in Bash
2,202
2009-10-05 17:52:54
<p>How do I iterate through each line of a text file with <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)" rel="noreferrer">Bash</a>?</p> <p>With this script:</p> <pre><code>echo "Start!" for p in (peptides.txt) do echo "${p}" done </code></pre> <p>I get this output on the screen:</p> <pre><code>Start!...
3,042,183
63,550
2025-12-03 13:38:30
1,521,498
3,056
2009-10-05 18:00:20
6,918
2019-10-31 17:28:04
https://stackoverflow.com/q/1521462
https://stackoverflow.com/a/1521498
<p>One way to do it is:</p> <pre><code>while read p; do echo "$p" done &lt;peptides.txt </code></pre> <p>As pointed out in the comments, this has the side effects of trimming leading whitespace, interpreting backslash sequences, and skipping the last line if it's missing a terminating linefeed. If these are concern...
<p>One way to do it is:</p> <pre><code>while read p; do echo "$p" done &lt;peptides.txt </code></pre> <p>As pointed out in the comments, this has the side effects of trimming leading whitespace, interpreting backslash sequences, and skipping the last line if it's missing a terminating linefeed. If these are concern...
34, 58, 345, 387, 10327
bash, io, linux, sh, unix
<h1>Looping through the content of a file in Bash</h1> <p>How do I iterate through each line of a text file with <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)" rel="noreferrer">Bash</a>?</p> <p>With this script:</p> <pre><code>echo "Start!" for p in (peptides.txt) do echo "${p}" done </code></pre> <p>...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
40
bash
# Looping through the content of a file in Bash How do I iterate through each line of a text file with [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell))? With this script: ``` echo "Start!" for p in (peptides.txt) do echo "${p}" done ``` I get this output on the screen: ``` Start! ./runPep.sh: line 3: sy...
One way to do it is: ``` while read p; do echo "$p" done <peptides.txt ``` As pointed out in the comments, this has the side effects of trimming leading whitespace, interpreting backslash sequences, and skipping the last line if it's missing a terminating linefeed. If these are concerns, you can do: ``` while IFS=...
7131670
Make a Bash alias that takes a parameter?
1,899
2011-08-20 12:11:41
<p>I used to use CShell (<a href="/questions/tagged/csh" class="post-tag" title="show questions tagged &#39;csh&#39;" rel="tag">csh</a>), which lets you make an alias that takes a parameter. The notation was something like</p> <pre><code>alias junk="mv \\!* ~/.Trash" </code></pre> <p>In Bash, this does not seem to wo...
997,593
902,361
2025-02-04 10:19:27
7,131,683
2,996
2011-08-20 12:15:04
203,454
2019-04-11 08:24:04
https://stackoverflow.com/q/7131670
https://stackoverflow.com/a/7131683
<p>Bash alias does not directly accept parameters. You will have to create a function.</p> <p><code>alias</code> does not accept parameters but a function can be called just like an alias. For example:</p> <pre><code>myfunction() { #do things with parameters like $1 such as mv "$1" "$1.bak" cp "$2" "$1" }...
<p>Bash alias does not directly accept parameters. You will have to create a function.</p> <p><code>alias</code> does not accept parameters but a function can be called just like an alias. For example:</p> <pre><code>myfunction() { #do things with parameters like $1 such as mv "$1" "$1.bak" cp "$2" "$1" }...
387, 1448
alias, bash
<h1>Make a Bash alias that takes a parameter?</h1> <p>I used to use CShell (<a href="/questions/tagged/csh" class="post-tag" title="show questions tagged &#39;csh&#39;" rel="tag">csh</a>), which lets you make an alias that takes a parameter. The notation was something like</p> <pre><code>alias junk="mv \\!* ~/.Trash" ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
41
bash
# Make a Bash alias that takes a parameter? I used to use CShell ([csh](/questions/tagged/csh "show questions tagged 'csh'")), which lets you make an alias that takes a parameter. The notation was something like ``` alias junk="mv \\!* ~/.Trash" ``` In Bash, this does not seem to work. Given that Bash has a multitud...
Bash alias does not directly accept parameters. You will have to create a function. `alias` does not accept parameters but a function can be called just like an alias. For example: ``` myfunction() { #do things with parameters like $1 such as mv "$1" "$1.bak" cp "$2" "$1" } myfunction old.conf new.conf ...
876239
How to redirect and append both standard output and standard error to a file with Bash
2,110
2009-05-18 04:19:45
<p>To redirect <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="noreferrer">standard output</a> to a truncated file in Bash, I know to use:</p> <pre><code>cmd &gt; file.txt </code></pre> <p>To redirect standard output in Bash, appending to a file, I know to use:</p> <pre><code>...
1,166,671
63,051
2023-11-19 09:42:16
876,242
2,670
2009-05-18 04:23:16
95,810
2017-03-09 14:55:58
https://stackoverflow.com/q/876239
https://stackoverflow.com/a/876242
<pre><code>cmd &gt;&gt;file.txt 2&gt;&amp;1 </code></pre> <p>Bash executes the redirects from left to right as follows:</p> <ol> <li><code>&gt;&gt;file.txt</code>: Open <code>file.txt</code> in append mode and redirect <code>stdout</code> there.</li> <li><code>2&gt;&amp;1</code>: Redirect <code>stderr</code> to <em>"...
<pre><code>cmd &gt;&gt;file.txt 2&gt;&amp;1 </code></pre> <p>Bash executes the redirects from left to right as follows:</p> <ol> <li><code>&gt;&gt;file.txt</code>: Open <code>file.txt</code> in append mode and redirect <code>stdout</code> there.</li> <li><code>2&gt;&amp;1</code>: Redirect <code>stderr</code> to <em>"...
387, 4867, 6051, 19156, 26698
append, bash, io-redirection, stderr, stdout
<h1>How to redirect and append both standard output and standard error to a file with Bash</h1> <p>To redirect <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="noreferrer">standard output</a> to a truncated file in Bash, I know to use:</p> <pre><code>cmd &gt; file.txt </code></...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
42
bash
# How to redirect and append both standard output and standard error to a file with Bash To redirect [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29) to a truncated file in Bash, I know to use: ``` cmd > file.txt ``` To redirect standard output in Bash, appending to a fi...
``` cmd >>file.txt 2>&1 ``` Bash executes the redirects from left to right as follows: 1. `>>file.txt`: Open `file.txt` in append mode and redirect `stdout` there. 2. `2>&1`: Redirect `stderr` to *"where `stdout` is currently going"*. In this case, that is a file opened in append mode. In other words, the `&1` reuses...
2013547
Assigning default values to shell variables with a single command in bash
1,467
2010-01-06 14:29:31
<p>I have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.:</p> <pre><code>if [ -z "${VARIABLE}" ]; then FOO='default' else FOO=${VARIABLE} fi </code></pre> <p>I seem to recall there's some syntax to doing this in one line, s...
984,074
87,408
2025-12-05 15:39:20
2,013,589
2,619
2010-01-06 14:36:56
241,774
2024-10-17 09:37:22
https://stackoverflow.com/q/2013547
https://stackoverflow.com/a/2013589
<p>Very close to what you posted, actually. You can use something called <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">Bash parameter expansion</a> to accomplish this.</p> <p>To get the assigned value, or <code>default</code> if it's missing:</p> <pre><code...
<p>Very close to what you posted, actually. You can use something called <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html" rel="noreferrer">Bash parameter expansion</a> to accomplish this.</p> <p>To get the assigned value, or <code>default</code> if it's missing:</p> <pre><code...
387, 390
bash, shell
<h1>Assigning default values to shell variables with a single command in bash</h1> <p>I have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.:</p> <pre><code>if [ -z "${VARIABLE}" ]; then FOO='default' else FOO=${VARIABLE} fi ...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
43
bash
# Assigning default values to shell variables with a single command in bash I have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.: ``` if [ -z "${VARIABLE}" ]; then FOO='default' else FOO=${VARIABLE} fi ``` I seem to recal...
Very close to what you posted, actually. You can use something called [Bash parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) to accomplish this. To get the assigned value, or `default` if it's missing: ``` FOO="${VARIABLE:-default}" # FOO will be assigned 'defau...
3137094
Count number of lines in a non binary file (Like a CSV or a TXT) file in terminal
1,458
2010-06-29 00:31:31
<p>I have a text file, and I like to know the total number of line withou opening it. My document is like these, and I want to know how many lines actually...</p> <pre><code>09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 94.00 09:16:40 AM all 5.00 0.00 0.00 4.00 0.00 ...
1,924,090
368,453
2024-09-13 12:33:01
3,137,099
2,601
2010-06-29 00:33:38
85,509
2015-04-29 19:27:45
https://stackoverflow.com/q/3137094
https://stackoverflow.com/a/3137099
<p>Use <code>wc</code>:</p> <pre><code>wc -l &lt;filename&gt; </code></pre> <p>This will output the number of lines in <code>&lt;filename&gt;</code>:</p> <pre><code>$ wc -l /dir/file.txt 3272485 /dir/file.txt </code></pre> <p>Or, to omit the <code>&lt;filename&gt;</code> from the result use <code>wc -l &lt; &lt;fil...
<p>Use <code>wc</code>:</p> <pre><code>wc -l &lt;filename&gt; </code></pre> <p>This will output the number of lines in <code>&lt;filename&gt;</code>:</p> <pre><code>$ wc -l /dir/file.txt 3272485 /dir/file.txt </code></pre> <p>Or, to omit the <code>&lt;filename&gt;</code> from the result use <code>wc -l &lt; &lt;fil...
58, 387, 531, 1231
bash, command-line, linux, scripting
<h1>Count number of lines in a non binary file (Like a CSV or a TXT) file in terminal</h1> <p>I have a text file, and I like to know the total number of line withou opening it. My document is like these, and I want to know how many lines actually...</p> <pre><code>09:16:39 AM all 2.00 0.00 4.00 0.00 0.0...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
44
bash
# Count number of lines in a non binary file (Like a CSV or a TXT) file in terminal I have a text file, and I like to know the total number of line withou opening it. My document is like these, and I want to know how many lines actually... ``` 09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 ...
Use `wc`: ``` wc -l <filename> ``` This will output the number of lines in `<filename>`: ``` $ wc -l /dir/file.txt 3272485 /dir/file.txt ``` Or, to omit the `<filename>` from the result use `wc -l < <filename>`: ``` $ wc -l < /dir/file.txt 3272485 ``` You can also pipe data to `wc` as well: ``` $ cat /dir/file.t...
169511
How do I iterate over a range of numbers defined by variables in Bash?
2,273
2008-10-04 01:38:43
<p>How do I iterate over a range of numbers in Bash when the range is given by a variable?</p> <p>I know I can do this (called &quot;sequence expression&quot; in the Bash <a href="http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion" rel="noreferrer">documentation</a>):</p> <pre><code>for i in {1..5}; d...
1,926,914
24,923
2024-10-05 16:49:03
169,517
2,507
2008-10-04 01:41:55
2,908
2016-04-23 00:41:06
https://stackoverflow.com/q/169511
https://stackoverflow.com/a/169517
<pre><code>for i in $(seq 1 $END); do echo $i; done</code></pre> <p>edit: I prefer <code>seq</code> over the other methods because I can actually remember it ;)</p>
<pre><code>for i in $(seq 1 $END); do echo $i; done</code></pre> <p>edit: I prefer <code>seq</code> over the other methods because I can actually remember it ;)</p>
367, 387, 390, 2531
bash, for-loop, shell, syntax
<h1>How do I iterate over a range of numbers defined by variables in Bash?</h1> <p>How do I iterate over a range of numbers in Bash when the range is given by a variable?</p> <p>I know I can do this (called &quot;sequence expression&quot; in the Bash <a href="http://www.gnu.org/software/bash/manual/bashref.html#Brace-E...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
45
bash
# How do I iterate over a range of numbers defined by variables in Bash? How do I iterate over a range of numbers in Bash when the range is given by a variable? I know I can do this (called "sequence expression" in the Bash [documentation](http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion)): ``` f...
``` for i in $(seq 1 $END); do echo $i; done ``` edit: I prefer `seq` over the other methods because I can actually remember it ;)
1401482
YYYY-MM-DD format date in shell script
1,617
2009-09-09 19:06:56
<p>I tried using <code>$(date)</code> in my bash shell script, however, I want the date in <code>YYYY-MM-DD</code> format.<br> How do I get this?</p>
2,304,731
45,730
2024-08-06 13:04:41
1,401,495
2,406
2009-09-09 19:08:57
11,123
2024-08-06 13:04:41
https://stackoverflow.com/q/1401482
https://stackoverflow.com/a/1401495
<p>In bash (&gt;=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external <code>date</code> (usually GNU date). Note that invoking a subshell has performance problems in Cygwin due to a slow <code>fork()</code> call on Windows.</p> <p>As such:</p> <pre><code># put current da...
<p>In bash (&gt;=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external <code>date</code> (usually GNU date). Note that invoking a subshell has performance problems in Cygwin due to a slow <code>fork()</code> call on Windows.</p> <p>As such:</p> <pre><code># put current da...
387, 390, 5002, 10823, 35771
bash, date, date-formatting, shell, strftime
<h1>YYYY-MM-DD format date in shell script</h1> <p>I tried using <code>$(date)</code> in my bash shell script, however, I want the date in <code>YYYY-MM-DD</code> format.<br> How do I get this?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
46
bash
# YYYY-MM-DD format date in shell script I tried using `$(date)` in my bash shell script, however, I want the date in `YYYY-MM-DD` format. How do I get this?
In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external `date` (usually GNU date). Note that invoking a subshell has performance problems in Cygwin due to a slow `fork()` call on Windows. As such: ``` # put current date as yyyy-mm-dd in $date # -1 -> explicit c...
6212219
Passing parameters to a Bash function
1,578
2011-06-02 08:35:17
<p>I am trying to search how to pass parameters in a Bash function, but what comes up is always how to pass parameter from the <em>command line</em>.</p> <p>I would like to pass parameters within my script. I tried:</p> <pre><code>myBackupFunction(&quot;..&quot;, &quot;...&quot;, &quot;xx&quot;) function myBackupFunct...
2,039,197
445,543
2024-11-20 15:20:36
6,212,408
2,399
2011-06-02 08:57:02
7,412
2020-12-18 16:27:37
https://stackoverflow.com/q/6212219
https://stackoverflow.com/a/6212408
<p>There are two typical ways of declaring a function. I prefer the second approach.</p> <pre><code>function function_name { command... } </code></pre> <p>or</p> <pre><code>function_name () { command... } </code></pre> <p>To call a function with arguments:</p> <pre><code>function_name &quot;$arg1&quot; &quot;$a...
<p>There are two typical ways of declaring a function. I prefer the second approach.</p> <pre><code>function function_name { command... } </code></pre> <p>or</p> <pre><code>function_name () { command... } </code></pre> <p>To call a function with arguments:</p> <pre><code>function_name &quot;$arg1&quot; &quot;$a...
360, 387, 2313, 5569
arguments, bash, function, parameters
<h1>Passing parameters to a Bash function</h1> <p>I am trying to search how to pass parameters in a Bash function, but what comes up is always how to pass parameter from the <em>command line</em>.</p> <p>I would like to pass parameters within my script. I tried:</p> <pre><code>myBackupFunction(&quot;..&quot;, &quot;......
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
47
bash
# Passing parameters to a Bash function I am trying to search how to pass parameters in a Bash function, but what comes up is always how to pass parameter from the *command line*. I would like to pass parameters within my script. I tried: ``` myBackupFunction("..", "...", "xx") function myBackupFunction($directory,...
There are two typical ways of declaring a function. I prefer the second approach. ``` function function_name { command... } ``` or ``` function_name () { command... } ``` To call a function with arguments: ``` function_name "$arg1" "$arg2" ``` The function refers to passed arguments by their position (not b...
13210880
Replace one substring for another string in shell script
1,457
2012-11-03 16:01:23
<p>I have &quot;I love Suzi and Marry&quot; and I want to change &quot;Suzi&quot; to &quot;Sara&quot;.</p> <pre><code>firstString=&quot;I love Suzi and Marry&quot; secondString=&quot;Sara&quot; </code></pre> <p>Desired result:</p> <pre><code>firstString=&quot;I love Sara and Marry&quot; </code></pre>
1,824,557
1,796,726
2024-06-17 13:39:36
13,210,909
2,391
2012-11-03 16:05:06
978,917
2024-06-17 13:39:36
https://stackoverflow.com/q/13210880
https://stackoverflow.com/a/13210909
<p>To replace the <em>first</em> occurrence of a pattern with a given string, use <code>${<em>parameter</em>/<em>pattern</em>/<em>string</em>}</code>:</p> <pre><code>#!/bin/bash firstString=&quot;I love Suzi and Marry&quot; secondString=&quot;Sara&quot; echo &quot;${firstString/Suzi/&quot;$secondString&quot;}&quot; # p...
<p>To replace the <em>first</em> occurrence of a pattern with a given string, use <code>${<em>parameter</em>/<em>pattern</em>/<em>string</em>}</code>:</p> <pre><code>#!/bin/bash firstString=&quot;I love Suzi and Marry&quot; secondString=&quot;Sara&quot; echo &quot;${firstString/Suzi/&quot;$secondString&quot;}&quot; # p...
387, 390
bash, shell
<h1>Replace one substring for another string in shell script</h1> <p>I have &quot;I love Suzi and Marry&quot; and I want to change &quot;Suzi&quot; to &quot;Sara&quot;.</p> <pre><code>firstString=&quot;I love Suzi and Marry&quot; secondString=&quot;Sara&quot; </code></pre> <p>Desired result:</p> <pre><code>firstString=...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
48
bash
# Replace one substring for another string in shell script I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara". ``` firstString="I love Suzi and Marry" secondString="Sara" ``` Desired result: ``` firstString="I love Sara and Marry" ```
To replace the *first* occurrence of a pattern with a given string, use `${parameter/pattern/string}`: ``` #!/bin/bash firstString="I love Suzi and Marry" secondString="Sara" echo "${firstString/Suzi/"$secondString"}" # prints 'I love Sara and Marry' ``` To replace *all* occurrences, use `${parameter//pattern/string}...
418896
How to redirect output to a file and stdout
1,677
2009-01-07 01:45:42
<p>In bash, calling <code>foo</code> would display any output from that command on the stdout.</p> <p>Calling <code>foo &gt; output</code> would redirect any output from that command to the file specified (in this case 'output').</p> <p>Is there a way to redirect output to a file <em>and</em> have it display on stdou...
1,246,622
1,666
2024-03-19 09:17:37
418,899
2,378
2009-01-07 01:48:08
20,267
2017-10-23 17:10:56
https://stackoverflow.com/q/418896
https://stackoverflow.com/a/418899
<p>The command you want is named <strong><a href="http://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html" rel="noreferrer"><code>tee</code></a></strong>:</p> <pre><code>foo | tee output.file </code></pre> <p>For example, if you only care about stdout:</p> <pre><code>ls -a | tee output.file </code...
<p>The command you want is named <strong><a href="http://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html" rel="noreferrer"><code>tee</code></a></strong>:</p> <pre><code>foo | tee output.file </code></pre> <p>For example, if you only care about stdout:</p> <pre><code>ls -a | tee output.file </code...
58, 345, 387, 724, 4867
bash, file-io, io, linux, stdout
<h1>How to redirect output to a file and stdout</h1> <p>In bash, calling <code>foo</code> would display any output from that command on the stdout.</p> <p>Calling <code>foo &gt; output</code> would redirect any output from that command to the file specified (in this case 'output').</p> <p>Is there a way to redirect o...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
49
bash
# How to redirect output to a file and stdout In bash, calling `foo` would display any output from that command on the stdout. Calling `foo > output` would redirect any output from that command to the file specified (in this case 'output'). Is there a way to redirect output to a file *and* have it display on stdout?
The command you want is named **[`tee`](http://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html)**: ``` foo | tee output.file ``` For example, if you only care about stdout: ``` ls -a | tee output.file ``` If you want to include stderr, do: ``` program [arguments...] 2>&1 | tee outfile ``` `2>&...
4608187
How to reload .bash_profile from the command line
1,234
2011-01-05 19:09:07
<p>How can I reload file <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example" rel="noreferrer">.bash_profile</a> from the <em>command line</em>?</p> <p>I can get the shell to recognize changes to <em>.bash_profile</em> by exiting and logging back in, but I would like to be ab...
1,217,536
97,101
2024-10-08 02:19:42
4,608,197
2,292
2011-01-05 19:10:03
207,248
2024-10-08 02:19:42
https://stackoverflow.com/q/4608187
https://stackoverflow.com/a/4608197
<p>Simply type <code>source ~/.bash_profile</code></p> <p>Alternatively, if you like saving keystrokes, you can type <code>. ~/.bash_profile</code></p>
<p>Simply type <code>source ~/.bash_profile</code></p> <p>Alternatively, if you like saving keystrokes, you can type <code>. ~/.bash_profile</code></p>
387, 390, 1231
bash, command-line, shell
<h1>How to reload .bash_profile from the command line</h1> <p>How can I reload file <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example" rel="noreferrer">.bash_profile</a> from the <em>command line</em>?</p> <p>I can get the shell to recognize changes to <em>.bash_profile</em...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
50
bash
# How to reload .bash_profile from the command line How can I reload file [.bash_profile](https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example) from the *command line*? I can get the shell to recognize changes to *.bash_profile* by exiting and logging back in, but I would like to be ...
Simply type `source ~/.bash_profile` Alternatively, if you like saving keystrokes, you can type `. ~/.bash_profile`
343646
Ignoring directories in Git repositories on Windows
1,592
2008-12-05 12:17:28
<p>How can I ignore directories or folders in Git using msysgit on Windows?</p>
2,050,509
43,603
2025-05-29 14:10:31
343,734
2,241
2008-12-05 12:54:10
43,613
2019-11-14 13:48:32
https://stackoverflow.com/q/343646
https://stackoverflow.com/a/343734
<p>Create a file named <code>.gitignore</code> in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):</p> <pre><code>dir_to_ignore/ </code></pre> <p>More information is <a href="http://git-scm.com/docs/gitignore" rel="noreferrer">here</a>.</p>
<p>Create a file named <code>.gitignore</code> in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):</p> <pre><code>dir_to_ignore/ </code></pre> <p>More information is <a href="http://git-scm.com/docs/gitignore" rel="noreferrer">here</a>.</p>
64, 119, 21628, 25056, 61874
git, git-bash, gitignore, msysgit, windows
<h1>Ignoring directories in Git repositories on Windows</h1> <p>How can I ignore directories or folders in Git using msysgit on Windows?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
51
bash
# Ignoring directories in Git repositories on Windows How can I ignore directories or folders in Git using msysgit on Windows?
Create a file named `.gitignore` in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended): ``` dir_to_ignore/ ``` More information is [here](http://git-scm.com/docs/gitignore).
2990414
echo that outputs to stderr
1,686
2010-06-07 14:36:16
<p>Is there a standard Bash command that acts like <code>echo</code> but outputs to stderr rather than stdout?</p> <p>I know I can do <code>echo foo 1&gt;&amp;2</code> but it's kinda ugly and, I suspect, error-prone (e.g. more likely to get edited wrong when things change).</p>
1,048,664
1,343
2023-10-10 06:11:19
23,550,347
2,201
2014-05-08 18:59:47
675,584
2023-05-09 22:29:35
https://stackoverflow.com/q/2990414
https://stackoverflow.com/a/23550347
<p>You could do this, which facilitates reading:</p> <pre><code>&gt;&amp;2 echo &quot;error&quot; </code></pre> <p><code>&gt;&amp;2</code> copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was <s...
<p>You could do this, which facilitates reading:</p> <pre><code>&gt;&amp;2 echo &quot;error&quot; </code></pre> <p><code>&gt;&amp;2</code> copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was <s...
387
bash
<h1>echo that outputs to stderr</h1> <p>Is there a standard Bash command that acts like <code>echo</code> but outputs to stderr rather than stdout?</p> <p>I know I can do <code>echo foo 1&gt;&amp;2</code> but it's kinda ugly and, I suspect, error-prone (e.g. more likely to get edited wrong when things change).</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
52
bash
# echo that outputs to stderr Is there a standard Bash command that acts like `echo` but outputs to stderr rather than stdout? I know I can do `echo foo 1>&2` but it's kinda ugly and, I suspect, error-prone (e.g. more likely to get edited wrong when things change).
You could do this, which facilitates reading: ``` >&2 echo "error" ``` `>&2` copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was **originally** referring to. For more information see the [Bas...
226703
How do I prompt for Yes/No/Cancel input in a Linux shell script?
1,921
2008-10-22 17:03:22
<p>I want to pause input in a shell script, and prompt the user for choices.<br> The standard <code>Yes</code>, <code>No</code>, or <code>Cancel</code> type question.<br> How do I accomplish this in a typical bash prompt?</p>
1,415,315
9,084
2026-01-06 20:54:44
226,724
2,092
2008-10-22 17:08:50
9,084
2025-03-18 14:21:37
https://stackoverflow.com/q/226703
https://stackoverflow.com/a/226724
<p>A widely available method to get user input at a shell prompt is the <a href="https://www.gnu.org/software/bash/manual/bashref.html#index-read" rel="noreferrer"><code>read</code></a> command. Here is a demonstration:</p> <pre><code>while true; do read -p &quot;Do you wish to install this program? &quot; yn c...
<p>A widely available method to get user input at a shell prompt is the <a href="https://www.gnu.org/software/bash/manual/bashref.html#index-read" rel="noreferrer"><code>read</code></a> command. Here is a demonstration:</p> <pre><code>while true; do read -p &quot;Do you wish to install this program? &quot; yn c...
58, 387, 390, 531
bash, linux, scripting, shell
<h1>How do I prompt for Yes/No/Cancel input in a Linux shell script?</h1> <p>I want to pause input in a shell script, and prompt the user for choices.<br> The standard <code>Yes</code>, <code>No</code>, or <code>Cancel</code> type question.<br> How do I accomplish this in a typical bash prompt?</p>
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
53
bash
# How do I prompt for Yes/No/Cancel input in a Linux shell script? I want to pause input in a shell script, and prompt the user for choices. The standard `Yes`, `No`, or `Cancel` type question. How do I accomplish this in a typical bash prompt?
A widely available method to get user input at a shell prompt is the [`read`](https://www.gnu.org/software/bash/manual/bashref.html#index-read) command. Here is a demonstration: ``` while true; do read -p "Do you wish to install this program? " yn case $yn in [Yy]* ) make install; break;; [Nn]*...
4824590
Propagate all arguments in a Bash shell script
1,291
2011-01-28 03:34:04
<p>I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.</p> <p>For instance, my script name is <code>foo.sh</code> and calls <code>bar.sh</code>.</p> <p>foo.sh:</p> <pre><code>bar $1 $2 $3 $4 </code></pre> <p>How can I d...
737,326
121,112
2026-01-14 22:40:28
4,824,637
2,024
2011-01-28 03:42:43
401,390
2026-01-14 22:40:28
https://stackoverflow.com/q/4824590
https://stackoverflow.com/a/4824637
<p>Use <code>&quot;$@&quot;</code> instead of plain <code>$@</code> if you actually wish your parameters to be passed exactly as is.</p> <p>Observe:</p> <pre class="lang-bash prettyprint-override"><code>$ cat no_quotes.sh #!/bin/bash ./echo_args.sh $@ $ cat quotes.sh #!/bin/bash ./echo_args.sh &quot;$@&quot; $ cat ec...
<p>Use <code>&quot;$@&quot;</code> instead of plain <code>$@</code> if you actually wish your parameters to be passed exactly as is.</p> <p>Observe:</p> <pre class="lang-bash prettyprint-override"><code>$ cat no_quotes.sh #!/bin/bash ./echo_args.sh $@ $ cat quotes.sh #!/bin/bash ./echo_args.sh &quot;$@&quot; $ cat ec...
387, 31134
bash, command-line-arguments
<h1>Propagate all arguments in a Bash shell script</h1> <p>I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.</p> <p>For instance, my script name is <code>foo.sh</code> and calls <code>bar.sh</code>.</p> <p>foo.sh:</p>...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
54
bash
# Propagate all arguments in a Bash shell script I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing. For instance, my script name is `foo.sh` and calls `bar.sh`. foo.sh: ``` bar $1 $2 $3 $4 ``` How can I do this wi...
Use `"$@"` instead of plain `$@` if you actually wish your parameters to be passed exactly as is. Observe: ``` $ cat no_quotes.sh #!/bin/bash ./echo_args.sh $@ $ cat quotes.sh #!/bin/bash ./echo_args.sh "$@" $ cat echo_args.sh #!/bin/bash echo Received: $1 echo Received: $2 echo Received: $3 echo Received: $4 $ ./...
255898
How to iterate over arguments in a Bash script
1,205
2008-11-01 18:14:21
<p>I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of <code>$1</code> easily:</p> <pre><code>foo $1 args -o $1.ext </code></pre> <p>I want to be able to pass multiple input names to the script. What's the right way to do it? </p> <p>And, of course, I want to handle fi...
968,520
12,874
2023-07-31 17:02:04
255,913
2,010
2008-11-01 18:25:27
25,222
2023-07-31 17:02:04
https://stackoverflow.com/q/255898
https://stackoverflow.com/a/255913
<p>Use <code>&quot;$@&quot;</code> to represent all the arguments:</p> <pre><code>for var in &quot;$@&quot; do echo &quot;$var&quot; done </code></pre> <p>This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that, when quoted, the arguments are broken up properly if t...
<p>Use <code>&quot;$@&quot;</code> to represent all the arguments:</p> <pre><code>for var in &quot;$@&quot; do echo &quot;$var&quot; done </code></pre> <p>This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that, when quoted, the arguments are broken up properly if t...
387, 1231
bash, command-line
<h1>How to iterate over arguments in a Bash script</h1> <p>I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of <code>$1</code> easily:</p> <pre><code>foo $1 args -o $1.ext </code></pre> <p>I want to be able to pass multiple input names to the script. What's the right way...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
55
bash
# How to iterate over arguments in a Bash script I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of `$1` easily: ``` foo $1 args -o $1.ext ``` I want to be able to pass multiple input names to the script. What's the right way to do it? And, of course, I want to handle ...
Use `"$@"` to represent all the arguments: ``` for var in "$@" do echo "$var" done ``` This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that, when quoted, the arguments are broken up properly if there are spaces in them: ``` sh test.sh 1 2 '3 4' 1 2 3 4 ```
1250079
How to escape single quotes within single quoted strings
1,494
2009-08-08 22:50:10
<p>Let's say, you have a Bash <code>alias</code> like:</p> <pre><code>alias rxvt='urxvt' </code></pre> <p>which works fine.</p> <p>However:</p> <pre class="lang-none prettyprint-override"><code>alias rxvt='urxvt -fg '#111111' -bg '#111111'' </code></pre> <p>won't work, and neither will:</p> <pre><code>alias rxvt=...
813,241
152,404
2025-05-29 14:58:14
1,250,279
2,002
2009-08-09 00:52:37
42,610
2024-03-11 23:33:18
https://stackoverflow.com/q/1250079
https://stackoverflow.com/a/1250279
<p>If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:</p> <pre><code> alias rxvt='urxvt -fg '&quot;'&quot;'#111111'&quot;'&quot;' -bg '&quot;'&quot;'#111111'&quot;'&quot; # ^^^^^ ^^^^^ ^^^^^ ^^^^ # ...
<p>If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:</p> <pre><code> alias rxvt='urxvt -fg '&quot;'&quot;'#111111'&quot;'&quot;' -bg '&quot;'&quot;'#111111'&quot;'&quot; # ^^^^^ ^^^^^ ^^^^^ ^^^^ # ...
367, 387, 10804
bash, quoting, syntax
<h1>How to escape single quotes within single quoted strings</h1> <p>Let's say, you have a Bash <code>alias</code> like:</p> <pre><code>alias rxvt='urxvt' </code></pre> <p>which works fine.</p> <p>However:</p> <pre class="lang-none prettyprint-override"><code>alias rxvt='urxvt -fg '#111111' -bg '#111111'' </code></...
q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10
56
bash
# How to escape single quotes within single quoted strings Let's say, you have a Bash `alias` like: ``` alias rxvt='urxvt' ``` which works fine. However: ``` alias rxvt='urxvt -fg '#111111' -bg '#111111'' ``` won't work, and neither will: ``` alias rxvt='urxvt -fg \'#111111\' -bg \'#111111\'' ``` So how do you ...
If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example: ``` alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'" # ^^^^^ ^^^^^ ^^^^^ ^^^^ # 12345 12345 12345 1234 ``` ...
End of preview. Expand in Data Studio

No dataset card yet

Downloads last month
13