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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
24102974 | How to Batch Rename Files in a macOS Terminal? | 157 | 2014-06-08 04:24:25 | <p>I have a folder with a series of files named:</p>
<pre><code>prefix_1234_567.png
prefix_abcd_efg.png
</code></pre>
<p>I'd like to <em>batch remove</em> one underscore and the middle content so the output would be:</p>
<pre><code>prefix_567.png
prefix_efg.png
</code></pre>
<p>Relevant but not completely explanato... | 171,298 | 1,775,952 | 2023-10-18 19:19:39 | 24,103,055 | 281 | 2014-06-08 04:39:42 | 45,375 | 2018-07-31 19:16:58 | https://stackoverflow.com/q/24102974 | https://stackoverflow.com/a/24103055 | <p>In your specific case you can use the following <code>bash</code> command (<code>bash</code> is the default shell on macOS):</p>
<pre><code>for f in *.png; do echo mv "$f" "${f/_*_/_}"; done
</code></pre>
<p><sup>Note: If there's a chance that your filenames start with <code>-</code>, place <code>--</code> before... | <p>In your specific case you can use the following <code>bash</code> command (<code>bash</code> is the default shell on macOS):</p> <pre><code>for f in *.png; do echo mv "$f" "${f/_*_/_}"; done </code></pre> <p><sup>Note: If there's a chance that your filenames start with <code>-</code>, place <code>--</code> before... | 369, 387, 390, 391, 12895 | bash, file-rename, macos, shell, terminal | <h1>How to Batch Rename Files in a macOS Terminal?</h1>
<p>I have a folder with a series of files named:</p>
<pre><code>prefix_1234_567.png
prefix_abcd_efg.png
</code></pre>
<p>I'd like to <em>batch remove</em> one underscore and the middle content so the output would be:</p>
<pre><code>prefix_567.png
prefix_efg.png... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 720 | bash | # How to Batch Rename Files in a macOS Terminal?
I have a folder with a series of files named:
```
prefix_1234_567.png
prefix_abcd_efg.png
```
I'd like to *batch remove* one underscore and the middle content so the output would be:
```
prefix_567.png
prefix_efg.png
```
Relevant but not completely explanatory:
- [... | In your specific case you can use the following `bash` command (`bash` is the default shell on macOS):
```
for f in *.png; do echo mv "$f" "${f/_*_/_}"; done
```
Note: If there's a chance that your filenames start with `-`, place `--` before them[1]:
`mv -- "$f" "${f/_*_/_}"`
**Note: `echo` is prepended to `mv` so... |
2859908 | Iterating over each line of ls -l output | 141 | 2010-05-18 18:24:24 | <p>I want to iterate over each line in the output of: <code>ls -l /some/dir/*</code></p>
<p>Right now I'm trying: <code>for x in $(ls -l $1); do echo $x; done</code></p>
<p>However, this iterates over each element in the line separately, so I get:</p>
<pre class="lang-none prettyprint-override"><code>-r--r-----
1
iv... | 129,230 | 247,722 | 2019-11-22 06:31:47 | 2,860,103 | 281 | 2010-05-18 18:50:40 | 22,131 | 2016-03-02 18:08:45 | https://stackoverflow.com/q/2859908 | https://stackoverflow.com/a/2860103 | <p>Set IFS to newline, like this:</p>
<pre><code>IFS='
'
for x in `ls -l $1`; do echo $x; done
</code></pre>
<p>Put a sub-shell around it if you don't want to set IFS permanently:</p>
<pre><code>(IFS='
'
for x in `ls -l $1`; do echo $x; done)
</code></pre>
<p>Or use while | read instead:</p>
<pre><code>ls -l $1 | ... | <p>Set IFS to newline, like this:</p> <pre><code>IFS=' ' for x in `ls -l $1`; do echo $x; done </code></pre> <p>Put a sub-shell around it if you don't want to set IFS permanently:</p> <pre><code>(IFS=' ' for x in `ls -l $1`; do echo $x; done) </code></pre> <p>Or use while | read instead:</p> <pre><code>ls -l $1 | ... | 58, 390 | linux, shell | <h1>Iterating over each line of ls -l output</h1>
<p>I want to iterate over each line in the output of: <code>ls -l /some/dir/*</code></p>
<p>Right now I'm trying: <code>for x in $(ls -l $1); do echo $x; done</code></p>
<p>However, this iterates over each element in the line separately, so I get:</p>
<pre class="lan... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 721 | bash | # Iterating over each line of ls -l output
I want to iterate over each line in the output of: `ls -l /some/dir/*`
Right now I'm trying: `for x in $(ls -l $1); do echo $x; done`
However, this iterates over each element in the line separately, so I get:
```
-r--r-----
1
ivanevf
eng
1074
Apr
22
13:07
File1
-r--r-----... | Set IFS to newline, like this:
```
IFS='
'
for x in `ls -l $1`; do echo $x; done
```
Put a sub-shell around it if you don't want to set IFS permanently:
```
(IFS='
'
for x in `ls -l $1`; do echo $x; done)
```
Or use while | read instead:
```
ls -l $1 | while read x; do echo $x; done
```
One more option, which run... |
13542832 | What is the difference between single and double square brackets in Bash? | 247 | 2012-11-24 15:56:58 | <p>I'm reading bash examples about <code>if</code>, but some examples are written with single square brackets:</p>
<pre><code>if [ -f $param ]
then
#...
fi
</code></pre>
<p>others with double square brackets:</p>
<pre><code>if [[ $? -ne 0 ]]
then
start looking for errors in yourlog
fi
</code></pre>
<p>What is the... | 104,840 | 816,721 | 2024-04-08 20:59:12 | 13,542,854 | 280 | 2012-11-24 15:59:25 | 1,331,457 | 2012-11-24 16:06:32 | https://stackoverflow.com/q/13542832 | https://stackoverflow.com/a/13542854 | <p>Single <code>[]</code> are posix shell compliant condition tests.</p>
<p>Double <code>[[]]</code> are an extension to the standard <code>[]</code> and are supported by bash and other shells (e.g. zsh, ksh). They support extra operations (as well as the standard posix operations). For example: <code>||</code> instea... | <p>Single <code>[]</code> are posix shell compliant condition tests.</p> <p>Double <code>[[]]</code> are an extension to the standard <code>[]</code> and are supported by bash and other shells (e.g. zsh, ksh). They support extra operations (as well as the standard posix operations). For example: <code>||</code> instea... | 387, 2773 | bash, if-statement | <h1>What is the difference between single and double square brackets in Bash?</h1>
<p>I'm reading bash examples about <code>if</code>, but some examples are written with single square brackets:</p>
<pre><code>if [ -f $param ]
then
#...
fi
</code></pre>
<p>others with double square brackets:</p>
<pre><code>if [[ $? -n... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 722 | bash | # What is the difference between single and double square brackets in Bash?
I'm reading bash examples about `if`, but some examples are written with single square brackets:
```
if [ -f $param ]
then
#...
fi
```
others with double square brackets:
```
if [[ $? -ne 0 ]]
then
start looking for errors in yourlog
... | Single `[]` are posix shell compliant condition tests.
Double `[[]]` are an extension to the standard `[]` and are supported by bash and other shells (e.g. zsh, ksh). They support extra operations (as well as the standard posix operations). For example: `||` instead of `-o` and regex matching with `=~`. A fuller list ... |
538664 | Using sed, how do you print the first 'N' characters of a line? | 152 | 2009-02-11 20:40:23 | <p>Using <code>sed</code> what is an one liner to print the first <em>n characters</em>? I am doing the following:</p>
<pre><code>grep -G 'defn -test.*' OctaneFullTest.clj | sed ....
</code></pre>
| 213,618 | 10,522 | 2024-07-05 14:01:38 | 538,676 | 280 | 2009-02-11 20:42:17 | 3,333 | 2020-01-13 13:43:02 | https://stackoverflow.com/q/538664 | https://stackoverflow.com/a/538676 | <p>Don't use <code>sed</code>, use <code>cut</code>:</p>
<pre><code>grep .... | cut -c 1-N
</code></pre>
<p>If you MUST use <code>sed</code>:</p>
<pre><code>grep ... | sed -e 's/^\(.\{12\}\).*/\1/'
</code></pre>
| <p>Don't use <code>sed</code>, use <code>cut</code>:</p> <pre><code>grep .... | cut -c 1-N </code></pre> <p>If you MUST use <code>sed</code>:</p> <pre><code>grep ... | sed -e 's/^\(.\{12\}\).*/\1/' </code></pre> | 390, 1271, 5282, 6067 | character, grep, sed, shell | <h1>Using sed, how do you print the first 'N' characters of a line?</h1>
<p>Using <code>sed</code> what is an one liner to print the first <em>n characters</em>? I am doing the following:</p>
<pre><code>grep -G 'defn -test.*' OctaneFullTest.clj | sed ....
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 723 | bash | # Using sed, how do you print the first 'N' characters of a line?
Using `sed` what is an one liner to print the first *n characters*? I am doing the following:
```
grep -G 'defn -test.*' OctaneFullTest.clj | sed ....
``` | Don't use `sed`, use `cut`:
```
grep .... | cut -c 1-N
```
If you MUST use `sed`:
```
grep ... | sed -e 's/^\(.\{12\}\).*/\1/'
``` |
18042279 | How to exit a function in bash | 199 | 2013-08-04 11:10:10 | <p>How would you exit out of a function if a condition is true without killing the whole script, just return back to before you called the function.</p>
<p>Example</p>
<pre><code># Start script
Do scripty stuff here
Ok now lets call FUNCT
FUNCT
Here is A to come back to
function FUNCT {
if [ blah is false ]; then
... | 253,574 | 2,272,450 | 2025-03-31 19:42:00 | 18,042,300 | 279 | 2013-08-04 11:12:57 | 1,246,772 | 2013-08-04 11:12:57 | https://stackoverflow.com/q/18042279 | https://stackoverflow.com/a/18042300 | <p>Use:</p>
<pre><code>return [n]
</code></pre>
<p>From <code>help return</code></p>
<blockquote>
<p><strong>return</strong>: return [n]</p>
<pre><code>Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last... | <p>Use:</p> <pre><code>return [n] </code></pre> <p>From <code>help return</code></p> <blockquote> <p><strong>return</strong>: return [n]</p> <pre><code>Return from a shell function. Causes a function or sourced script to exit with the return value specified by N. If N is omitted, the return status is that of the last... | 387, 5569, 10871 | bash, exit, function | <h1>How to exit a function in bash</h1>
<p>How would you exit out of a function if a condition is true without killing the whole script, just return back to before you called the function.</p>
<p>Example</p>
<pre><code># Start script
Do scripty stuff here
Ok now lets call FUNCT
FUNCT
Here is A to come back to
functi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 724 | bash | # How to exit a function in bash
How would you exit out of a function if a condition is true without killing the whole script, just return back to before you called the function.
Example
```
# Start script
Do scripty stuff here
Ok now lets call FUNCT
FUNCT
Here is A to come back to
function FUNCT {
if [ blah is f... | Use:
```
return [n]
```
From `help return`
> **return**: return [n]
>
> ```
> Return from a shell function.
>
> Causes a function or sourced script to exit with the return value
> specified by N. If N is omitted, the return status is that of the
> last command executed within the function or script.
>
> Exit Status... |
1954203 | Timestamp on file name using PowerShell | 148 | 2009-12-23 17:32:25 | <p>I have a path in a string,</p>
<pre><code>C:\temp\mybackup.zip
</code></pre>
<p>I would like insert a timestamp in that script, for example,</p>
<pre><code>C:\temp\mybackup 2009-12-23.zip
</code></pre>
<p>Is there an easy way to do this in PowerShell?</p>
| 472,739 | 105,553 | 2023-02-21 10:31:09 | 1,954,384 | 279 | 2009-12-23 18:03:42 | 153,982 | 2016-12-14 20:13:29 | https://stackoverflow.com/q/1954203 | https://stackoverflow.com/a/1954384 | <p>You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:</p>
<pre><code>"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
</code></pre>
<p>And if you are getting the path from somewhere else - already as a string:</p>
<pre><code>$dirName = [io.p... | <p>You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:</p> <pre><code>"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip" </code></pre> <p>And if you are getting the path from somewhere else - already as a string:</p> <pre><code>$dirName = [io.p... | 526 | powershell | <h1>Timestamp on file name using PowerShell</h1>
<p>I have a path in a string,</p>
<pre><code>C:\temp\mybackup.zip
</code></pre>
<p>I would like insert a timestamp in that script, for example,</p>
<pre><code>C:\temp\mybackup 2009-12-23.zip
</code></pre>
<p>Is there an easy way to do this in PowerShell?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 725 | bash | # Timestamp on file name using PowerShell
I have a path in a string,
```
C:\temp\mybackup.zip
```
I would like insert a timestamp in that script, for example,
```
C:\temp\mybackup 2009-12-23.zip
```
Is there an easy way to do this in PowerShell? | You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:
```
"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"
```
And if you are getting the path from somewhere else - already as a string:
```
$dirName = [io.path]::GetDirectoryName($path)
$filenam... |
4162821 | Bash foreach loop | 207 | 2010-11-12 08:33:54 | <p>I have an input (let's say a file).
On each line there is a file name.
How can I read this file and display the content for each one.</p>
| 447,513 | 357,314 | 2020-01-13 20:01:27 | 4,162,829 | 278 | 2010-11-12 08:35:33 | 893 | 2010-11-12 08:35:33 | https://stackoverflow.com/q/4162821 | https://stackoverflow.com/a/4162829 | <p>Something like this would do:</p>
<pre><code>xargs cat <filenames.txt
</code></pre>
<p>The <code>xargs</code> program reads its standard input, and for each line of input runs the <code>cat</code> program with the input lines as argument(s).</p>
<p>If you really want to do this in a loop, you can:</p>
<pre><c... | <p>Something like this would do:</p> <pre><code>xargs cat <filenames.txt </code></pre> <p>The <code>xargs</code> program reads its standard input, and for each line of input runs the <code>cat</code> program with the input lines as argument(s).</p> <p>If you really want to do this in a loop, you can:</p> <pre><c... | 387, 631 | bash, foreach | <h1>Bash foreach loop</h1>
<p>I have an input (let's say a file).
On each line there is a file name.
How can I read this file and display the content for each one.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 726 | bash | # Bash foreach loop
I have an input (let's say a file).
On each line there is a file name.
How can I read this file and display the content for each one. | Something like this would do:
```
xargs cat <filenames.txt
```
The `xargs` program reads its standard input, and for each line of input runs the `cat` program with the input lines as argument(s).
If you really want to do this in a loop, you can:
```
for fn in `cat filenames.txt`; do
echo "the next file is $fn"
... |
4286469 | How to parse a CSV file in Bash? | 159 | 2010-11-26 15:20:32 | <p>I'm working on a long Bash script. I want to read cells from a CSV file into Bash variables. I can parse lines and the first column, but not any other column. Here's my code so far:</p>
<pre><code>
cat myfile.csv|while read line
do
read -d, col1 col2 < <(echo $line)
echo "I got:$col1|$col2"
d... | 263,693 | 125,380 | 2024-02-19 12:31:14 | 4,286,841 | 278 | 2010-11-26 16:09:45 | 26,428 | 2023-01-11 17:34:44 | https://stackoverflow.com/q/4286469 | https://stackoverflow.com/a/4286841 | <p>You need to use <code>IFS</code> instead of <code>-d</code>:</p>
<pre><code>while IFS=, read -r col1 col2
do
echo "I got:$col1|$col2"
done < myfile.csv
</code></pre>
<p>To skip a given number of header lines:</p>
<pre class="lang-bash prettyprint-override"><code>skip_headers=3
while IFS=, read -r co... | <p>You need to use <code>IFS</code> instead of <code>-d</code>:</p> <pre><code>while IFS=, read -r col1 col2 do echo "I got:$col1|$col2" done < myfile.csv </code></pre> <p>To skip a given number of header lines:</p> <pre class="lang-bash prettyprint-override"><code>skip_headers=3 while IFS=, read -r co... | 58, 73, 387 | bash, csv, linux | <h1>How to parse a CSV file in Bash?</h1>
<p>I'm working on a long Bash script. I want to read cells from a CSV file into Bash variables. I can parse lines and the first column, but not any other column. Here's my code so far:</p>
<pre><code>
cat myfile.csv|while read line
do
read -d, col1 col2 < <(ec... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 727 | bash | # How to parse a CSV file in Bash?
I'm working on a long Bash script. I want to read cells from a CSV file into Bash variables. I can parse lines and the first column, but not any other column. Here's my code so far:
```
cat myfile.csv|while read line
do
read -d, col1 col2 < <(echo $line)
echo "I got:$col... | You need to use `IFS` instead of `-d`:
```
while IFS=, read -r col1 col2
do
echo "I got:$col1|$col2"
done < myfile.csv
```
To skip a given number of header lines:
```
skip_headers=3
while IFS=, read -r col1 col2
do
if ((skip_headers))
then
((skip_headers--))
else
echo "I got:$col1|$co... |
9806944 | grep only text files | 83 | 2012-03-21 14:47:04 | <pre><code>find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}
</code></pre>
<p>it's a good solution? for find TEXTSEARCH recursively in only textual files</p>
| 70,694 | 526,444 | 2023-04-21 18:08:02 | 9,807,012 | 278 | 2012-03-21 14:51:32 | 348,785 | 2015-04-15 10:29:44 | https://stackoverflow.com/q/9806944 | https://stackoverflow.com/a/9807012 | <p>You can use the <code>-r</code>(recursive) and <code>-I</code>(ignore binary) options in <code>grep</code>:</p>
<pre><code>$ grep -rI "TEXTSEARCH" .
</code></pre>
<blockquote>
<ul>
<li><code>-I</code> Process a binary file as if it did not contain matching data; this is equivalent to the <code>--binary-fil... | <p>You can use the <code>-r</code>(recursive) and <code>-I</code>(ignore binary) options in <code>grep</code>:</p> <pre><code>$ grep -rI "TEXTSEARCH" . </code></pre> <blockquote> <ul> <li><code>-I</code> Process a binary file as if it did not contain matching data; this is equivalent to the <code>--binary-fil... | 387, 1271 | bash, grep | <h1>grep only text files</h1>
<pre><code>find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}
</code></pre>
<p>it's a good solution? for find TEXTSEARCH recursively in only textual files</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 728 | bash | # grep only text files
```
find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}
```
it's a good solution? for find TEXTSEARCH recursively in only textual files | You can use the `-r`(recursive) and `-I`(ignore binary) options in `grep`:
```
$ grep -rI "TEXTSEARCH" .
```
> - `-I` Process a binary file as if it did not contain matching data; this is equivalent to the `--binary-files=without-match` option.
> - `-r` Read all files under each directory, recursively; this is equiva... |
3592851 | Executing a command stored in a variable from PowerShell | 200 | 2010-08-28 23:05:58 | <p>I have a command that I have build and stored in a variable in PowerShell. This command works if I do a <a href="https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Write-Host?view=powershell-5.1https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Write-Host" ... | 295,937 | 433,296 | 2019-11-18 04:35:34 | 3,593,445 | 277 | 2010-08-29 03:51:49 | 323,582 | 2014-07-15 07:49:45 | https://stackoverflow.com/q/3592851 | https://stackoverflow.com/a/3593445 | <p>Here is yet another way without <code>Invoke-Expression</code> but with two variables
(command:string and parameters:array). It works fine for me. Assume
<code>7z.exe</code> is in the system path.</p>
<pre class="lang-sh prettyprint-override"><code>$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\... | <p>Here is yet another way without <code>Invoke-Expression</code> but with two variables (command:string and parameters:array). It works fine for me. Assume <code>7z.exe</code> is in the system path.</p> <pre class="lang-sh prettyprint-override"><code>$cmd = '7z.exe' $prm = 'a', '-tzip', 'c:\temp\with space\... | 526 | powershell | <h1>Executing a command stored in a variable from PowerShell</h1>
<p>I have a command that I have build and stored in a variable in PowerShell. This command works if I do a <a href="https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Write-Host?view=powershell-5.1https://learn.microsoft.com... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 729 | bash | # Executing a command stored in a variable from PowerShell
I have a command that I have build and stored in a variable in PowerShell. This command works if I do a [Write-Host](https://learn.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Write-Host?view=powershell-5.1https://learn.microsoft.com/en-g... | Here is yet another way without `Invoke-Expression` but with two variables
(command:string and parameters:array). It works fine for me. Assume
`7z.exe` is in the system path.
```
$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& $cmd $prm
```
If the command is know... |
8624669 | unix - head AND tail of file | 178 | 2011-12-24 12:59:07 | <p>What is the command to view the top 10 lines and bottom 10 lines of file simultaneously?</p>
| 88,711 | 688,412 | 2025-10-06 23:16:26 | 8,624,829 | 277 | 2011-12-24 13:29:41 | 1,030,462 | 2018-08-29 14:17:28 | https://stackoverflow.com/q/8624669 | https://stackoverflow.com/a/8624829 | <p>You can simply:</p>
<pre><code>(head; tail) < file.txt
</code></pre>
<p>And if you need to uses pipes for some reason then like this:</p>
<pre><code>cat file.txt | (head; tail)
</code></pre>
<p>Note: will print duplicated lines if number of lines in file.txt is smaller than default lines of head + default lin... | <p>You can simply:</p> <pre><code>(head; tail) < file.txt </code></pre> <p>And if you need to uses pipes for some reason then like this:</p> <pre><code>cat file.txt | (head; tail) </code></pre> <p>Note: will print duplicated lines if number of lines in file.txt is smaller than default lines of head + default lin... | 34, 58, 387, 390, 531 | bash, linux, scripting, shell, unix | <h1>unix - head AND tail of file</h1>
<p>What is the command to view the top 10 lines and bottom 10 lines of file simultaneously?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 730 | bash | # unix - head AND tail of file
What is the command to view the top 10 lines and bottom 10 lines of file simultaneously? | You can simply:
```
(head; tail) < file.txt
```
And if you need to uses pipes for some reason then like this:
```
cat file.txt | (head; tail)
```
Note: will print duplicated lines if number of lines in file.txt is smaller than default lines of head + default lines of tail. |
6153152 | Find files containing a given text | 209 | 2011-05-27 13:50:29 | <p>In bash I want to return file name (and the path to the file) for every file of type <code>.php|.html|.js</code> containing the case-insensitive string <code>"document.cookie" | "setcookie"</code></p>
<p>How would I do that?</p>
| 209,340 | 328,765 | 2019-08-18 19:23:43 | 6,153,342 | 276 | 2011-05-27 14:03:22 | 253,650 | 2019-08-18 19:23:43 | https://stackoverflow.com/q/6153152 | https://stackoverflow.com/a/6153342 | <pre><code>egrep -ir --include=*.{php,html,js} "(document.cookie|setcookie)" .
</code></pre>
<p>The <code>r</code> flag means to search recursively (search subdirectories). The <code>i</code> flag means case insensitive.</p>
<p>If you just want file names add the <code>l</code> (lowercase <code>L</code>) flag:</p>
<... | <pre><code>egrep -ir --include=*.{php,html,js} "(document.cookie|setcookie)" . </code></pre> <p>The <code>r</code> flag means to search recursively (search subdirectories). The <code>i</code> flag means case insensitive.</p> <p>If you just want file names add the <code>l</code> (lowercase <code>L</code>) flag:</p> <... | 387, 10193 | bash, find | <h1>Find files containing a given text</h1>
<p>In bash I want to return file name (and the path to the file) for every file of type <code>.php|.html|.js</code> containing the case-insensitive string <code>"document.cookie" | "setcookie"</code></p>
<p>How would 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 | 731 | bash | # Find files containing a given text
In bash I want to return file name (and the path to the file) for every file of type `.php|.html|.js` containing the case-insensitive string `"document.cookie" | "setcookie"`
How would I do that? | ```
egrep -ir --include=*.{php,html,js} "(document.cookie|setcookie)" .
```
The `r` flag means to search recursively (search subdirectories). The `i` flag means case insensitive.
If you just want file names add the `l` (lowercase `L`) flag:
```
egrep -lir --include=*.{php,html,js} "(document.cookie|setcookie)" .
``` |
4544709 | Compare two files line by line and generate the difference in another file | 162 | 2010-12-28 08:32:32 | <p>I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2.</p>
| 485,181 | 534,908 | 2022-11-29 21:59:02 | 4,546,712 | 276 | 2010-12-28 14:20:27 | 180,736 | 2017-07-08 15:00:26 | https://stackoverflow.com/q/4544709 | https://stackoverflow.com/a/4546712 | <p>diff(1) is not the answer, but comm(1) is.</p>
<pre><code>NAME
comm - compare two sorted files line by line
SYNOPSIS
comm [OPTION]... FILE1 FILE2
...
-1 suppress lines unique to FILE1
-2 suppress lines unique to FILE2
-3 suppress lines that appear in both files
</... | <p>diff(1) is not the answer, but comm(1) is.</p> <pre><code>NAME comm - compare two sorted files line by line SYNOPSIS comm [OPTION]... FILE1 FILE2 ... -1 suppress lines unique to FILE1 -2 suppress lines unique to FILE2 -3 suppress lines that appear in both files </... | 34, 390 | shell, unix | <h1>Compare two files line by line and generate the difference in another file</h1>
<p>I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 732 | bash | # Compare two files line by line and generate the difference in another file
I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2. | diff(1) is not the answer, but comm(1) is.
```
NAME
comm - compare two sorted files line by line
SYNOPSIS
comm [OPTION]... FILE1 FILE2
...
-1 suppress lines unique to FILE1
-2 suppress lines unique to FILE2
-3 suppress lines that appear in both files
```
So
```
com... |
192292 | How best to include other scripts? | 466 | 2008-10-10 17:14:38 | <p>The way you would normally include a script is with "source"</p>
<p>eg:</p>
<p>main.sh:</p>
<pre><code>#!/bin/bash
source incl.sh
echo "The main script"
</code></pre>
<p>incl.sh:</p>
<pre><code>echo "The included script"
</code></pre>
<p>The output of executing "./main.sh" is:</p>
<pre><code>The included sc... | 437,668 | 16,258 | 2023-01-24 01:21:08 | 192,305 | 275 | 2008-10-10 17:17:52 | 25,660 | 2014-05-31 09:21:58 | https://stackoverflow.com/q/192292 | https://stackoverflow.com/a/192305 | <p>I tend to make my scripts all be relative to one another.
That way I can use dirname:</p>
<pre><code>#!/bin/sh
my_dir="$(dirname "$0")"
"$my_dir/other_script.sh"
</code></pre>
| <p>I tend to make my scripts all be relative to one another. That way I can use dirname:</p> <pre><code>#!/bin/sh my_dir="$(dirname "$0")" "$my_dir/other_script.sh" </code></pre> | 387 | bash | <h1>How best to include other scripts?</h1>
<p>The way you would normally include a script is with "source"</p>
<p>eg:</p>
<p>main.sh:</p>
<pre><code>#!/bin/bash
source incl.sh
echo "The main script"
</code></pre>
<p>incl.sh:</p>
<pre><code>echo "The included script"
</code></pre>
<p>The output of executing "./... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 733 | bash | # How best to include other scripts?
The way you would normally include a script is with "source"
eg:
main.sh:
```
#!/bin/bash
source incl.sh
echo "The main script"
```
incl.sh:
```
echo "The included script"
```
The output of executing "./main.sh" is:
```
The included script
The main script
```
... Now, if ... | I tend to make my scripts all be relative to one another.
That way I can use dirname:
```
#!/bin/sh
my_dir="$(dirname "$0")"
"$my_dir/other_script.sh"
``` |
1455988 | Commenting in a Bash script inside a multiline command | 229 | 2009-09-21 18:17:26 | <p>How can I comment on each line of the following lines from a script?</p>
<pre><code>cat ${MYSQLDUMP} | \
sed '1d' | \
tr ",;" "\n" | \
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
sed -e '/^option_id/d' -e '/^print_value/d' -e '... | 335,338 | 174,648 | 2025-09-28 18:45:39 | 1,456,019 | 275 | 2009-09-21 18:21:18 | 140,740 | 2024-03-29 09:37:01 | https://stackoverflow.com/q/1455988 | https://stackoverflow.com/a/1456019 | <p>This will have some overhead, but technically it does answer your question:</p>
<pre><code>echo abc `#Put your comment here` \
def `#Another chance for a comment` \
xyz, etc.
</code></pre>
<p>And for pipelines specifically, there is a clean solution with no overhead:</p>
<pre><code>echo abc | # Norm... | <p>This will have some overhead, but technically it does answer your question:</p> <pre><code>echo abc `#Put your comment here` \ def `#Another chance for a comment` \ xyz, etc. </code></pre> <p>And for pipelines specifically, there is a clean solution with no overhead:</p> <pre><code>echo abc | # Norm... | 367, 387, 1966 | bash, comments, syntax | <h1>Commenting in a Bash script inside a multiline command</h1>
<p>How can I comment on each line of the following lines from a script?</p>
<pre><code>cat ${MYSQLDUMP} | \
sed '1d' | \
tr ",;" "\n" | \
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
sed -n -e '/^"/p' -e '/^print_value$/,/^op... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 734 | bash | # Commenting in a Bash script inside a multiline command
How can I comment on each line of the following lines from a script?
```
cat ${MYSQLDUMP} | \
sed '1d' | \
tr ",;" "\n" | \
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
sed ... | This will have some overhead, but technically it does answer your question:
```
echo abc `#Put your comment here` \
def `#Another chance for a comment` \
xyz, etc.
```
And for pipelines specifically, there is a clean solution with no overhead:
```
echo abc | # Normal comment OK here
tr a-z A-Z ... |
10574684 | Where to place $PATH variable assertions in zsh? | 206 | 2012-05-13 19:48:22 | <p>I love <code>zsh</code>, but I am not sure where to place my <code>$PATH</code> and other variable assertions? I find that they are scattered between the files <code>.zshrc</code> <code>.zprofile</code> <code>.bashrc</code> <code>.bash_profile</code>, and sometimes doubled.</p>
<p>I realize that having anything ins... | 316,406 | 708,274 | 2017-11-27 13:13:02 | 10,583,324 | 275 | 2012-05-14 12:27:05 | 1,084,945 | 2017-11-27 13:13:02 | https://stackoverflow.com/q/10574684 | https://stackoverflow.com/a/10583324 | <p>tl;dr version: use <code>~/.zshrc</code></p>
<p>And <a href="http://linux.die.net/man/1/zshall" rel="noreferrer">read the man page</a> to understand the differences between: </p>
<blockquote>
<p><code>~/.zshrc</code>, <code>~/.zshenv</code> and <code>~/.zprofile</code>. </p>
</blockquote>
<hr>
<h2>Regarding my... | <p>tl;dr version: use <code>~/.zshrc</code></p> <p>And <a href="http://linux.die.net/man/1/zshall" rel="noreferrer">read the man page</a> to understand the differences between: </p> <blockquote> <p><code>~/.zshrc</code>, <code>~/.zshenv</code> and <code>~/.zprofile</code>. </p> </blockquote> <hr> <h2>Regarding my... | 369, 390, 3791 | macos, shell, zsh | <h1>Where to place $PATH variable assertions in zsh?</h1>
<p>I love <code>zsh</code>, but I am not sure where to place my <code>$PATH</code> and other variable assertions? I find that they are scattered between the files <code>.zshrc</code> <code>.zprofile</code> <code>.bashrc</code> <code>.bash_profile</code>, and som... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 735 | bash | # Where to place $PATH variable assertions in zsh?
I love `zsh`, but I am not sure where to place my `$PATH` and other variable assertions? I find that they are scattered between the files `.zshrc` `.zprofile` `.bashrc` `.bash_profile`, and sometimes doubled.
I realize that having anything inside the `bash` files doe... | tl;dr version: use `~/.zshrc`
And [read the man page](http://linux.die.net/man/1/zshall) to understand the differences between:
> `~/.zshrc`, `~/.zshenv` and `~/.zprofile`.
---
## Regarding my comment
In my comment attached to the answer [kev gave](https://stackoverflow.com/a/10576306/1084945), I said:
> This see... |
1491514 | Exclude .svn directories from grep | 194 | 2009-09-29 09:33:41 | <p>When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories?</p>
| 62,497 | 79,505 | 2021-02-28 06:22:07 | 1,491,543 | 275 | 2009-09-29 09:38:55 | 174,133 | 2012-01-04 23:52:30 | https://stackoverflow.com/q/1491514 | https://stackoverflow.com/a/1491543 | <p>If you have GNU Grep, it should work like this:</p>
<pre><code>grep --exclude-dir=".svn"
</code></pre>
<p>If happen to be on a Unix System without GNU Grep, try the following:</p>
<pre><code>grep -R "whatever you like" *|grep -v "\.svn/*"
</code></pre>
| <p>If you have GNU Grep, it should work like this:</p> <pre><code>grep --exclude-dir=".svn" </code></pre> <p>If happen to be on a Unix System without GNU Grep, try the following:</p> <pre><code>grep -R "whatever you like" *|grep -v "\.svn/*" </code></pre> | 58, 63, 387, 1271 | bash, grep, linux, svn | <h1>Exclude .svn directories from grep</h1>
<p>When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 736 | bash | # Exclude .svn directories from grep
When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories? | If you have GNU Grep, it should work like this:
```
grep --exclude-dir=".svn"
```
If happen to be on a Unix System without GNU Grep, try the following:
```
grep -R "whatever you like" *|grep -v "\.svn/*"
``` |
10247472 | How to prevent rm from reporting that a file was not found? | 236 | 2012-04-20 13:43:00 | <p>I am using <code>rm</code> within a BASH script to delete many files. Sometimes the files are not present, so it reports many errors. I do not need this message. I have searched the man page for a command to make <code>rm</code> quiet, but the only option I found is <code>-f</code>, which from the description, "igno... | 189,770 | 834,616 | 2018-04-27 17:37:31 | 10,250,395 | 273 | 2012-04-20 16:58:13 | 1,126,841 | 2018-04-27 17:37:31 | https://stackoverflow.com/q/10247472 | https://stackoverflow.com/a/10250395 | <p>The main use of <code>-f</code> is to force the removal of files that would
not be removed using <code>rm</code> by itself (as a special case, it "removes"
non-existent files, thus suppressing the error message).</p>
<p>You can also just redirect the error message using</p>
<pre><code>$ rm file.txt 2> /dev/null... | <p>The main use of <code>-f</code> is to force the removal of files that would not be removed using <code>rm</code> by itself (as a special case, it "removes" non-existent files, thus suppressing the error message).</p> <p>You can also just redirect the error message using</p> <pre><code>$ rm file.txt 2> /dev/null... | 387, 5610 | bash, rm | <h1>How to prevent rm from reporting that a file was not found?</h1>
<p>I am using <code>rm</code> within a BASH script to delete many files. Sometimes the files are not present, so it reports many errors. I do not need this message. I have searched the man page for a command to make <code>rm</code> quiet, but the only... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 737 | bash | # How to prevent rm from reporting that a file was not found?
I am using `rm` within a BASH script to delete many files. Sometimes the files are not present, so it reports many errors. I do not need this message. I have searched the man page for a command to make `rm` quiet, but the only option I found is `-f`, which ... | The main use of `-f` is to force the removal of files that would
not be removed using `rm` by itself (as a special case, it "removes"
non-existent files, thus suppressing the error message).
You can also just redirect the error message using
```
$ rm file.txt 2> /dev/null
```
(or your operating system's equivalent).... |
12404661 | What is the use case of noop [:] in bash? | 204 | 2012-09-13 10:55:05 | <p>I searched for noop in bash (:), but was not able to find any good information. What is the exact purpose or use case of this operator?</p>
<p>I tried following and it's working like this for me:</p>
<pre><code>[mandy@root]$ a=11
[mandy@root]$ b=20
[mandy@root]$ c=30
[mandy@root]$ echo $a; : echo $b ; echo $c
10
3... | 93,408 | 673,018 | 2025-07-07 07:49:25 | 12,405,621 | 273 | 2012-09-13 11:51:13 | 387,076 | 2012-09-13 11:51:13 | https://stackoverflow.com/q/12404661 | https://stackoverflow.com/a/12405621 | <p>It's there more for historical reasons. The colon builtin <code>:</code> is exactly equivalent to <code>true</code>. It's traditional to use <code>true</code> when the return value is important, for example in an infinite loop:</p>
<pre><code>while true; do
echo 'Going on forever'
done
</code></pre>
<p>It's trad... | <p>It's there more for historical reasons. The colon builtin <code>:</code> is exactly equivalent to <code>true</code>. It's traditional to use <code>true</code> when the return value is important, for example in an infinite loop:</p> <pre><code>while true; do echo 'Going on forever' done </code></pre> <p>It's trad... | 387, 390, 39458 | bash, no-op, shell | <h1>What is the use case of noop [:] in bash?</h1>
<p>I searched for noop in bash (:), but was not able to find any good information. What is the exact purpose or use case of this operator?</p>
<p>I tried following and it's working like this for me:</p>
<pre><code>[mandy@root]$ a=11
[mandy@root]$ b=20
[mandy@root]$ c... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 738 | bash | # What is the use case of noop [:] in bash?
I searched for noop in bash (:), but was not able to find any good information. What is the exact purpose or use case of this operator?
I tried following and it's working like this for me:
```
[mandy@root]$ a=11
[mandy@root]$ b=20
[mandy@root]$ c=30
[mandy@root]$ echo $a; ... | It's there more for historical reasons. The colon builtin `:` is exactly equivalent to `true`. It's traditional to use `true` when the return value is important, for example in an infinite loop:
```
while true; do
echo 'Going on forever'
done
```
It's traditional to use `:` when the shell syntax requires a command ... |
8110530 | Check free disk space for current partition in Bash | 184 | 2011-11-13 08:37:16 | <p>I am writing an installer in Bash. The user will go to the target directory and run the install script, so the first action should be to check that there is enough space. I know that <code>df</code> will report all file systems, but I was wondering if there was a way to get the free space just for the partition that... | 319,863 | 8,163 | 2025-08-10 22:08:34 | 8,110,535 | 273 | 2011-11-13 08:38:59 | 635,608 | 2011-11-13 08:38:59 | https://stackoverflow.com/q/8110530 | https://stackoverflow.com/a/8110535 | <p>Yes:</p>
<pre><code>df -k .
</code></pre>
<p>for the current directory.</p>
<pre><code>df -k /some/dir
</code></pre>
<p>if you want to check a specific directory.</p>
<p>You might also want to check out the <code>stat(1)</code> command if your system has it. You can specify output formats to make it easier for... | <p>Yes:</p> <pre><code>df -k . </code></pre> <p>for the current directory.</p> <pre><code>df -k /some/dir </code></pre> <p>if you want to check a specific directory.</p> <p>You might also want to check out the <code>stat(1)</code> command if your system has it. You can specify output formats to make it easier for... | 58, 387 | bash, linux | <h1>Check free disk space for current partition in Bash</h1>
<p>I am writing an installer in Bash. The user will go to the target directory and run the install script, so the first action should be to check that there is enough space. I know that <code>df</code> will report all file systems, but I was wondering if ther... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 739 | bash | # Check free disk space for current partition in Bash
I am writing an installer in Bash. The user will go to the target directory and run the install script, so the first action should be to check that there is enough space. I know that `df` will report all file systems, but I was wondering if there was a way to get t... | Yes:
```
df -k .
```
for the current directory.
```
df -k /some/dir
```
if you want to check a specific directory.
You might also want to check out the `stat(1)` command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:
```
$ echo $(($(stat ... |
26223936 | Open a Web Page in a Windows Batch FIle | 166 | 2014-10-06 19:59:55 | <p>I have a <strong>batch file</strong> that does a bunch of things and at the end needs to <strong>open up a web browser</strong> to a page. Is there a way to, in essence, call <code>ShellExecute</code> on a <strong>http</strong> to open the web page?</p>
<p>Windows Command Prompt</p>
| 447,881 | 172,861 | 2024-09-25 21:19:49 | 26,223,995 | 273 | 2014-10-06 20:04:31 | 3,826,372 | 2024-09-25 20:35:46 | https://stackoverflow.com/q/26223936 | https://stackoverflow.com/a/26223995 | <p>You can use the <a href="https://ss64.com/nt/start.html" rel="noreferrer">start</a> command to do much the same thing as <code>ShellExecute</code>. For example</p>
<pre><code> start "" http://www.stackoverflow.com
</code></pre>
<p>This will launch whatever browser is the default browser, so it won't necess... | <p>You can use the <a href="https://ss64.com/nt/start.html" rel="noreferrer">start</a> command to do much the same thing as <code>ShellExecute</code>. For example</p> <pre><code> start "" http://www.stackoverflow.com </code></pre> <p>This will launch whatever browser is the default browser, so it won't necess... | 64, 2631, 7002, 7839 | batch-file, cmd, shellexecute, windows | <h1>Open a Web Page in a Windows Batch FIle</h1>
<p>I have a <strong>batch file</strong> that does a bunch of things and at the end needs to <strong>open up a web browser</strong> to a page. Is there a way to, in essence, call <code>ShellExecute</code> on a <strong>http</strong> to open the web page?</p>
<p>Windows Co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 740 | bash | # Open a Web Page in a Windows Batch FIle
I have a **batch file** that does a bunch of things and at the end needs to **open up a web browser** to a page. Is there a way to, in essence, call `ShellExecute` on a **http** to open the web page?
Windows Command Prompt | You can use the [start](https://ss64.com/nt/start.html) command to do much the same thing as `ShellExecute`. For example
```
start "" http://www.stackoverflow.com
```
This will launch whatever browser is the default browser, so it won't necessarily launch Internet Explorer. |
19274127 | How do you grep a file and get the next 5 lines | 149 | 2013-10-09 13:55:53 | <p>How do I <code>grep</code> a file for <code>19:55</code> and get the Line 1,2,3,4,5?</p>
<pre><code>2013/10/08 19:55:27.471
Line 1
Line 2
Line 3
Line 4
Line 5
2013/10/08 19:55:29.566
Line 1
Line 2
Line 3
Line 4
Line 5
</code></pre>
| 178,137 | 2,530,127 | 2018-10-10 10:35:31 | 19,274,215 | 273 | 2013-10-09 13:59:13 | 1,066,031 | 2013-10-09 13:59:13 | https://stackoverflow.com/q/19274127 | https://stackoverflow.com/a/19274215 | <p>You want: </p>
<pre><code>grep -A 5 '19:55' file
</code></pre>
<p>From <code>man grep</code>:</p>
<pre><code>Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a gup separator (described under --group-separator)
between contiguo... | <p>You want: </p> <pre><code>grep -A 5 '19:55' file </code></pre> <p>From <code>man grep</code>:</p> <pre><code>Context Line Control -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a gup separator (described under --group-separator) between contiguo... | 390, 1271 | grep, shell | <h1>How do you grep a file and get the next 5 lines</h1>
<p>How do I <code>grep</code> a file for <code>19:55</code> and get the Line 1,2,3,4,5?</p>
<pre><code>2013/10/08 19:55:27.471
Line 1
Line 2
Line 3
Line 4
Line 5
2013/10/08 19:55:29.566
Line 1
Line 2
Line 3
Line 4
Line 5
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 741 | bash | # How do you grep a file and get the next 5 lines
How do I `grep` a file for `19:55` and get the Line 1,2,3,4,5?
```
2013/10/08 19:55:27.471
Line 1
Line 2
Line 3
Line 4
Line 5
2013/10/08 19:55:29.566
Line 1
Line 2
Line 3
Line 4
Line 5
``` | You want:
```
grep -A 5 '19:55' file
```
From `man grep`:
```
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a gup separator (described under --group-separator)
between contiguous groups of matches. With the -o or --only-match... |
21186724 | Why is whitespace sometimes needed around metacharacters? | 550 | 2014-01-17 13:03:05 | <p>A few months ago I tattooed a <a href="http://en.wikipedia.org/wiki/Fork_bomb" rel="noreferrer">fork bomb</a> on my arm, and I skipped the whitespaces, because I think it looks nicer without them. But to my dismay, <strong>sometimes</strong> (not always) when I run it in a shell it doesn't start a fork bomb, but it ... | 81,672 | 789,545 | 2018-08-01 13:58:24 | 21,187,095 | 271 | 2014-01-17 13:20:54 | 1,328,439 | 2014-06-09 15:16:07 | https://stackoverflow.com/q/21186724 | https://stackoverflow.com/a/21187095 | <p>There is a list of characters that separate tokens in BASH. These characters are called <em>metacharacters</em> and they are <code>|</code>, <code>&</code>, <code>;</code>, <code>(</code>, <code>)</code>, <code><</code>, <code>></code>, <strong>space</strong> and <strong>tab</strong>. On the other hand, cu... | <p>There is a list of characters that separate tokens in BASH. These characters are called <em>metacharacters</em> and they are <code>|</code>, <code>&</code>, <code>;</code>, <code>(</code>, <code>)</code>, <code><</code>, <code>></code>, <strong>space</strong> and <strong>tab</strong>. On the other hand, cu... | 367, 387, 390, 9993 | bash, shell, syntax, syntax-error | <h1>Why is whitespace sometimes needed around metacharacters?</h1>
<p>A few months ago I tattooed a <a href="http://en.wikipedia.org/wiki/Fork_bomb" rel="noreferrer">fork bomb</a> on my arm, and I skipped the whitespaces, because I think it looks nicer without them. But to my dismay, <strong>sometimes</strong> (not alw... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 742 | bash | # Why is whitespace sometimes needed around metacharacters?
A few months ago I tattooed a [fork bomb](http://en.wikipedia.org/wiki/Fork_bomb) on my arm, and I skipped the whitespaces, because I think it looks nicer without them. But to my dismay, **sometimes** (not always) when I run it in a shell it doesn't start a f... | There is a list of characters that separate tokens in BASH. These characters are called *metacharacters* and they are `|`, `&`, `;`, `(`, `)`, `<`, `>`, **space** and **tab**. On the other hand, curly braces (`{` and `}`) are just ordinary characters that make up words.
Omitting the second space before `}` will do, si... |
5342832 | How to append the output to a file? | 161 | 2011-03-17 17:28:42 | <p>How can I do something like <code>command > file</code> in a way that it appends to the file, instead of overwriting?</p>
| 185,860 | 287,316 | 2024-03-25 11:22:49 | 5,342,855 | 271 | 2011-03-17 17:30:12 | 656,769 | 2012-07-08 12:48:34 | https://stackoverflow.com/q/5342832 | https://stackoverflow.com/a/5342855 | <p>Use <code>>></code> to append:</p>
<pre><code>command >> file
</code></pre>
| <p>Use <code>>></code> to append:</p> <pre><code>command >> file </code></pre> | 58, 390, 1231 | command-line, linux, shell | <h1>How to append the output to a file?</h1>
<p>How can I do something like <code>command > file</code> in a way that it appends to the file, instead of overwriting?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 743 | bash | # How to append the output to a file?
How can I do something like `command > file` in a way that it appends to the file, instead of overwriting? | Use `>>` to append:
```
command >> file
``` |
4856583 | How do I pipe a subprocess call to a text file? | 157 | 2011-01-31 21:54:25 | <pre class="lang-py prettyprint-override"><code>subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml", "/tmp/video_xml"])
</code></pre>
<p>RIght now I have a script that I run. When I run it and it hits this line, it starts printing stuff because run.sh has prints in it.</p>
<p>How do I p... | 224,348 | 179,736 | 2025-09-14 18:08:16 | 4,856,684 | 271 | 2011-01-31 22:04:47 | 12,183 | 2021-07-16 21:30:01 | https://stackoverflow.com/q/4856583 | https://stackoverflow.com/a/4856684 | <p>If you want to write the output to a file you can use the <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen" rel="noreferrer">stdout</a>-argument of <code>subprocess.call</code>.</p>
<p>It takes either</p>
<ul>
<li><code>None</code> (the default, stdout is inherited from the parent (your scrip... | <p>If you want to write the output to a file you can use the <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen" rel="noreferrer">stdout</a>-argument of <code>subprocess.call</code>.</p> <p>It takes either</p> <ul> <li><code>None</code> (the default, stdout is inherited from the parent (your scrip... | 16, 390, 2348 | python, shell, subprocess | <h1>How do I pipe a subprocess call to a text file?</h1>
<pre class="lang-py prettyprint-override"><code>subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml", "/tmp/video_xml"])
</code></pre>
<p>RIght now I have a script that I run. When I run it and it hits this line, it starts printing... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 744 | bash | # How do I pipe a subprocess call to a text file?
```
subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml", "/tmp/video_xml"])
```
RIght now I have a script that I run. When I run it and it hits this line, it starts printing stuff because run.sh has prints in it.
How do I pipe this to a text file also? (And also p... | If you want to write the output to a file you can use the [stdout](http://docs.python.org/library/subprocess.html#subprocess.Popen)-argument of `subprocess.call`.
It takes either
- `None` (the default, stdout is inherited from the parent (your script))
- `subprocess.PIPE` (allows you to pipe from one command/process ... |
9018723 | What is the simplest way to remove a trailing slash from each parameter? | 175 | 2012-01-26 13:24:35 | <p>What is the simplest way to remove a trailing slash from each parameter in the '$@' array, so that <code>rsync</code> copies the directories by name?</p>
<pre><code>rsync -a --exclude='*~' "$@" "$dir"
</code></pre>
<hr>
<p><em>The title has been changed for clarification. To understand the comments and answer ab... | 155,482 | 198,183 | 2024-02-22 02:25:56 | 9,018,877 | 270 | 2012-01-26 13:34:33 | null | 2012-01-26 13:41:48 | https://stackoverflow.com/q/9018723 | https://stackoverflow.com/a/9018877 | <p>You can use the <code>${parameter%word}</code> expansion that is detailed <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">here</a>. Here is a simple test script that demonstrates the behavior:</p>
<pre><code>#!/bin/bash
# Call this as:
# ./test.sh one/ t... | <p>You can use the <code>${parameter%word}</code> expansion that is detailed <a href="http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion" rel="noreferrer">here</a>. Here is a simple test script that demonstrates the behavior:</p> <pre><code>#!/bin/bash # Call this as: # ./test.sh one/ t... | 387, 390, 1159, 2313, 37164 | arguments, bash, rsync, shell, stripslashes | <h1>What is the simplest way to remove a trailing slash from each parameter?</h1>
<p>What is the simplest way to remove a trailing slash from each parameter in the '$@' array, so that <code>rsync</code> copies the directories by name?</p>
<pre><code>rsync -a --exclude='*~' "$@" "$dir"
</code></pre>
<hr>
<p><em>The ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 745 | bash | # What is the simplest way to remove a trailing slash from each parameter?
What is the simplest way to remove a trailing slash from each parameter in the '$@' array, so that `rsync` copies the directories by name?
```
rsync -a --exclude='*~' "$@" "$dir"
```
---
*The title has been changed for clarification. To unde... | You can use the `${parameter%word}` expansion that is detailed [here](http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion). Here is a simple test script that demonstrates the behavior:
```
#!/bin/bash
# Call this as:
# ./test.sh one/ two/ three/
#
# Output:
# one two three
echo ${@%/}
... |
36388465 | How can I set Bash aliases for docker containers in Dockerfile? | 154 | 2016-04-03 17:09:22 | <p>I am new to Docker. I found that we can set environment variables using the <em>ENV</em> instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile?</p>
| 170,423 | 1,494,559 | 2022-04-23 07:52:03 | 36,388,856 | 270 | 2016-04-03 17:44:40 | 5,731,095 | 2021-11-09 02:56:32 | https://stackoverflow.com/q/36388465 | https://stackoverflow.com/a/36388856 | <p>Basically like you always do, by adding it to the user's <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example" rel="noreferrer"><code>.bashrc</code></a> file:</p>
<pre class="lang-none prettyprint-override"><code>FROM foo
RUN echo 'alias hi="echo hello"' >> ... | <p>Basically like you always do, by adding it to the user's <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example" rel="noreferrer"><code>.bashrc</code></a> file:</p> <pre class="lang-none prettyprint-override"><code>FROM foo RUN echo 'alias hi="echo hello"' >> ... | 34, 387, 1448, 90304, 108477 | alias, bash, docker, dockerfile, unix | <h1>How can I set Bash aliases for docker containers in Dockerfile?</h1>
<p>I am new to Docker. I found that we can set environment variables using the <em>ENV</em> instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 746 | bash | # How can I set Bash aliases for docker containers in Dockerfile?
I am new to Docker. I found that we can set environment variables using the *ENV* instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile? | Basically like you always do, by adding it to the user's [`.bashrc`](https://en.wikipedia.org/wiki/Bash_(Unix_shell)#Legacy-compatible_Bash_startup_example) file:
```
FROM foo
RUN echo 'alias hi="echo hello"' >> ~/.bashrc
```
As usual this will only work for interactive shells:
```
docker build -t test .
docker run ... |
16823591 | How to add lines to end of file on Linux | 144 | 2013-05-29 20:53:27 | <p>I want to add the following 2 lines:</p>
<pre class="lang-sh prettyprint-override"><code>VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
</code></pre>
<p>to the end of the file <code>vncservers</code> found at the directory <code>/etc/sysconfig/</code>.</p>
<p>How can I do this?</p>
| 236,060 | 1,898,806 | 2020-10-13 13:10:04 | 16,824,861 | 270 | 2013-05-29 22:19:28 | 897,079 | 2020-10-13 10:27:34 | https://stackoverflow.com/q/16823591 | https://stackoverflow.com/a/16824861 | <p>The easiest way is to redirect the output of the <code>echo</code> by <code>>></code>:</p>
<pre class="lang-sh prettyprint-override"><code>echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile
echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile
</code></... | <p>The easiest way is to redirect the output of the <code>echo</code> by <code>>></code>:</p> <pre class="lang-sh prettyprint-override"><code>echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile </code></... | 58, 390 | linux, shell | <h1>How to add lines to end of file on Linux</h1>
<p>I want to add the following 2 lines:</p>
<pre class="lang-sh prettyprint-override"><code>VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
</code></pre>
<p>to the end of the file <code>vncservers</code> found at the directory <code>/etc/s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 747 | bash | # How to add lines to end of file on Linux
I want to add the following 2 lines:
```
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
```
to the end of the file `vncservers` found at the directory `/etc/sysconfig/`.
How can I do this? | The easiest way is to redirect the output of the `echo` by `>>`:
```
echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile
echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile
``` |
11307465 | DESTDIR and PREFIX of make | 184 | 2012-07-03 08:49:48 | <p>I am trying to make software install to a specific directory. I found several ways, but not sure what are the differences between them.</p>
<ol>
<li><code>./configure --prefix=***</code></li>
<li><code>make install DESTDIR=***</code></li>
<li><code>make install prefix=***</code></li>
</ol>
<p>I am confused about t... | 142,327 | 1,311,827 | 2025-02-19 02:40:04 | 11,307,770 | 269 | 2012-07-03 09:09:19 | 1,493,754 | 2025-02-19 02:40:04 | https://stackoverflow.com/q/11307465 | https://stackoverflow.com/a/11307770 | <blockquote>
<p><code>./configure --prefix=***</code></p>
</blockquote>
<p><strong>Number 1</strong> determines where the package will go when it is installed, and where it will look for its associated files when it is run. It's what you should use if you're just compiling something for use on a single host.</p>
<hr />... | <blockquote> <p><code>./configure --prefix=***</code></p> </blockquote> <p><strong>Number 1</strong> determines where the package will go when it is installed, and where it will look for its associated files when it is run. It's what you should use if you're just compiling something for use on a single host.</p> <hr />... | 8, 58, 387, 4301, 8319 | bash, c, configure, linux, makefile | <h1>DESTDIR and PREFIX of make</h1>
<p>I am trying to make software install to a specific directory. I found several ways, but not sure what are the differences between them.</p>
<ol>
<li><code>./configure --prefix=***</code></li>
<li><code>make install DESTDIR=***</code></li>
<li><code>make install prefix=***</code><... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 748 | bash | # DESTDIR and PREFIX of make
I am trying to make software install to a specific directory. I found several ways, but not sure what are the differences between them.
1. `./configure --prefix=***`
2. `make install DESTDIR=***`
3. `make install prefix=***`
I am confused about the functions of these three. Do they achie... | > `./configure --prefix=***`
**Number 1** determines where the package will go when it is installed, and where it will look for its associated files when it is run. It's what you should use if you're just compiling something for use on a single host.
---
> `make install DESTDIR=***`
**Number 2** is for installing t... |
41130310 | Change directory in PowerShell | 172 | 2016-12-13 20:50:15 | <p>My PowerShell prompt's currently pointed to my C drive (<code>PS C:\></code>). How do I change directory to a folder on my Q (<code>PS Q:\></code>) drive?</p>
<p>The folder name on my Q drive is "My Test Folder".</p>
| 911,994 | 7,179,909 | 2025-04-09 02:35:38 | 41,130,386 | 269 | 2016-12-13 20:55:29 | 6,083,222 | 2019-10-19 18:32:46 | https://stackoverflow.com/q/41130310 | https://stackoverflow.com/a/41130386 | <p>Unlike the CMD.EXE <code>CHDIR</code> or <code>CD</code> command, the PowerShell <code>Set-Location</code> cmdlet will change drive and directory, both. <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-5.1" rel="noreferrer"><code>Get-Help Set-L... | <p>Unlike the CMD.EXE <code>CHDIR</code> or <code>CD</code> command, the PowerShell <code>Set-Location</code> cmdlet will change drive and directory, both. <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-5.1" rel="noreferrer"><code>Get-Help Set-L... | 526 | powershell | <h1>Change directory in PowerShell</h1>
<p>My PowerShell prompt's currently pointed to my C drive (<code>PS C:\></code>). How do I change directory to a folder on my Q (<code>PS Q:\></code>) drive?</p>
<p>The folder name on my Q drive is "My Test Folder".</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 749 | bash | # Change directory in PowerShell
My PowerShell prompt's currently pointed to my C drive (`PS C:\>`). How do I change directory to a folder on my Q (`PS Q:\>`) drive?
The folder name on my Q drive is "My Test Folder". | Unlike the CMD.EXE `CHDIR` or `CD` command, the PowerShell `Set-Location` cmdlet will change drive and directory, both. [`Get-Help Set-Location -Full`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-5.1) will get you more detailed information on `Set-Loc... |
53229221 | Terminal error: zsh: permission denied: ./startup.sh | 142 | 2018-11-09 16:01:15 | <p>I am running a command</p>
<pre><code>./startup.sh nginx:start
</code></pre>
<p>and I am getting this error message</p>
<pre><code>zsh: permission denied: ./startup.sh
</code></pre>
<p>why could this be happening?</p>
| 381,877 | 3,424,817 | 2025-12-01 18:44:00 | 53,229,323 | 269 | 2018-11-09 16:07:44 | 3,279,175 | 2020-05-29 12:32:41 | https://stackoverflow.com/q/53229221 | https://stackoverflow.com/a/53229323 | <p>Be sure to give it the execution permission.</p>
<pre><code>cd ~/the/script/folder
chmod +x ./startup.sh
</code></pre>
<p>This will give exec permission to user, group and other, so beware of possible security issues. To restrict permission to a single access class, you can use:</p>
<pre><code>chmod u+x ./startu... | <p>Be sure to give it the execution permission.</p> <pre><code>cd ~/the/script/folder chmod +x ./startup.sh </code></pre> <p>This will give exec permission to user, group and other, so beware of possible security issues. To restrict permission to a single access class, you can use:</p> <pre><code>chmod u+x ./startu... | 369, 391, 6605, 10327 | macos, nginx, sh, terminal | <h1>Terminal error: zsh: permission denied: ./startup.sh</h1>
<p>I am running a command</p>
<pre><code>./startup.sh nginx:start
</code></pre>
<p>and I am getting this error message</p>
<pre><code>zsh: permission denied: ./startup.sh
</code></pre>
<p>why could this be happening?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 750 | bash | # Terminal error: zsh: permission denied: ./startup.sh
I am running a command
```
./startup.sh nginx:start
```
and I am getting this error message
```
zsh: permission denied: ./startup.sh
```
why could this be happening? | Be sure to give it the execution permission.
```
cd ~/the/script/folder
chmod +x ./startup.sh
```
This will give exec permission to user, group and other, so beware of possible security issues. To restrict permission to a single access class, you can use:
```
chmod u+x ./startup.sh
```
This will grant exec permiss... |
11003418 | Calling shell functions with xargs | 286 | 2012-06-12 19:23:12 | <p>I am trying to use xargs to call a more complex function in parallel. </p>
<pre><code>#!/bin/bash
echo_var(){
echo $1
return 0
}
seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {}
exit 0
</code></pre>
<p>This returns the error</p>
<pre><code>xargs: echo_var: No such file or directory
</code></pre>
<... | 108,388 | 1,148,366 | 2024-04-11 16:17:33 | 11,003,457 | 268 | 2012-06-12 19:26:02 | 26,428 | 2019-08-18 17:16:09 | https://stackoverflow.com/q/11003418 | https://stackoverflow.com/a/11003457 | <p>Exporting the function should do it (untested):</p>
<pre><code>export -f echo_var
seq -f "n%04g" 1 100 | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {}
</code></pre>
<p>You can use the builtin <code>printf</code> instead of the external <code>seq</code>:</p>
<pre><code>printf "n%04g\n" {1..100} | xargs -n 1 ... | <p>Exporting the function should do it (untested):</p> <pre><code>export -f echo_var seq -f "n%04g" 1 100 | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {} </code></pre> <p>You can use the builtin <code>printf</code> instead of the external <code>seq</code>:</p> <pre><code>printf "n%04g\n" {1..100} | xargs -n 1 ... | 387, 10326, 10327 | bash, sh, xargs | <h1>Calling shell functions with xargs</h1>
<p>I am trying to use xargs to call a more complex function in parallel. </p>
<pre><code>#!/bin/bash
echo_var(){
echo $1
return 0
}
seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {}
exit 0
</code></pre>
<p>This returns the error</p>
<pre><code>xargs: echo_var... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 751 | bash | # Calling shell functions with xargs
I am trying to use xargs to call a more complex function in parallel.
```
#!/bin/bash
echo_var(){
echo $1
return 0
}
seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {}
exit 0
```
This returns the error
```
xargs: echo_var: No such file or directory
```
Any ideas on ... | Exporting the function should do it (untested):
```
export -f echo_var
seq -f "n%04g" 1 100 | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {}
```
You can use the builtin `printf` instead of the external `seq`:
```
printf "n%04g\n" {1..100} | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {}
```
Also, using `r... |
13939038 | How do you run a command eg chmod, for each line of a file? | 270 | 2012-12-18 18:17:24 | <p>For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file:</p>
<pre><code>cat file.txt | while read in; do chmod 755 "$in"; done
</code></pre>
<p>Is there a more elegant, safer way?</p>
| 305,875 | 1,274,311 | 2025-04-04 15:08:56 | 13,941,223 | 268 | 2012-12-18 20:46:22 | 1,765,658 | 2025-04-04 15:08:56 | https://stackoverflow.com/q/13939038 | https://stackoverflow.com/a/13941223 | <h1>Read a file line by line and execute commands: 4+ answers</h1>
<p>Because the main usage of <a href="/questions/tagged/shell" class="s-tag post-tag" title="show questions tagged 'shell'" aria-label="show questions tagged 'shell'" rel="tag" aria-labelledby="tag-shell-tooltip-container" data-tag-menu-... | <h1>Read a file line by line and execute commands: 4+ answers</h1> <p>Because the main usage of <a href="/questions/tagged/shell" class="s-tag post-tag" title="show questions tagged 'shell'" aria-label="show questions tagged 'shell'" rel="tag" aria-labelledby="tag-shell-tooltip-container" data-tag-menu-... | 387, 2314, 9325 | bash, line, loops | <h1>How do you run a command eg chmod, for each line of a file?</h1>
<p>For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file:</p>
<pre><code>cat file.txt | while read in; do chmod 755 "$in"; done
</code></pre>
<p>Is there a more elegant, safer way?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 752 | bash | # How do you run a command eg chmod, for each line of a file?
For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file:
```
cat file.txt | while read in; do chmod 755 "$in"; done
```
Is there a more elegant, safer way? | # Read a file line by line and execute commands: 4+ answers
Because the main usage of [shell](/questions/tagged/shell "show questions tagged 'shell'") ( and others shells like [bash](/questions/tagged/bash "show questions tagged 'bash'")) is to run other commands, there is not only 1 answer!!
0. Shell command lin... |
4499910 | How to display a specific user's commits in svn log? | 168 | 2010-12-21 13:48:33 | <p>How to display a specific user's commits in svn? I didn't find any switches for that for svn log.</p>
| 113,033 | 440,723 | 2018-07-11 18:10:34 | 4,500,627 | 268 | 2010-12-21 15:04:13 | 404,863 | 2016-04-05 14:23:29 | https://stackoverflow.com/q/4499910 | https://stackoverflow.com/a/4500627 | <p>You could use this:</p>
<pre><code>svn log | sed -n '/USERNAME/,/-----$/ p'
</code></pre>
<p>It will show you every commit made by the specified user (USERNAME).</p>
<p><strong>UPDATE</strong></p>
<p>As suggested by @bahrep, <a href="http://svnbook.red-bean.com/en/1.8/svn.ref.svn.html#svn.ref.svn.sw.search" rel... | <p>You could use this:</p> <pre><code>svn log | sed -n '/USERNAME/,/-----$/ p' </code></pre> <p>It will show you every commit made by the specified user (USERNAME).</p> <p><strong>UPDATE</strong></p> <p>As suggested by @bahrep, <a href="http://svnbook.red-bean.com/en/1.8/svn.ref.svn.html#svn.ref.svn.sw.search" rel... | 63, 387, 456 | bash, svn, version-control | <h1>How to display a specific user's commits in svn log?</h1>
<p>How to display a specific user's commits in svn? I didn't find any switches for that for svn log.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 753 | bash | # How to display a specific user's commits in svn log?
How to display a specific user's commits in svn? I didn't find any switches for that for svn log. | You could use this:
```
svn log | sed -n '/USERNAME/,/-----$/ p'
```
It will show you every commit made by the specified user (USERNAME).
**UPDATE**
As suggested by @bahrep, [subversion 1.8](http://svnbook.red-bean.com/en/1.8/svn.ref.svn.html#svn.ref.svn.sw.search) comes with a `--search` option. |
8746227 | How to open Emacs inside Bash | 161 | 2012-01-05 16:43:22 | <p>I'm using <a href="https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Ubuntu 11.10</a> (Oneiric Ocelot). When I type the command "emacs" in the terminal, it opens Emacs as a separate window. How can I open it inside the terminal, like the <a href="https://en.wikipedia.org/wiki/GNU_nano" ... | 151,542 | 1,132,633 | 2022-08-18 14:43:32 | 8,746,243 | 268 | 2012-01-05 16:44:48 | 128,645 | 2020-11-11 14:24:12 | https://stackoverflow.com/q/8746227 | https://stackoverflow.com/a/8746243 | <p>Emacs takes many launch options. The one that you are looking for is
<code>emacs -nw</code>. This will open Emacs inside the terminal disregarding the <em>DISPLAY</em> environment variable even if it is set.
The long form of this flag is <code>emacs --no-window-system</code>.</p>
<p>More information about Emacs laun... | <p>Emacs takes many launch options. The one that you are looking for is <code>emacs -nw</code>. This will open Emacs inside the terminal disregarding the <em>DISPLAY</em> environment variable even if it is set. The long form of this flag is <code>emacs --no-window-system</code>.</p> <p>More information about Emacs laun... | 371, 387 | bash, emacs | <h1>How to open Emacs inside Bash</h1>
<p>I'm using <a href="https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29" rel="noreferrer">Ubuntu 11.10</a> (Oneiric Ocelot). When I type the command "emacs" in the terminal, it opens Emacs as a separate window. How can I open it inside the terminal, like the <a href="h... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 754 | bash | # How to open Emacs inside Bash
I'm using [Ubuntu 11.10](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) (Oneiric Ocelot). When I type the command "emacs" in the terminal, it opens Emacs as a separate window. How can I open it inside the terminal, like the [nano](https://en.wikipedia.org/wiki/GNU_nano) editor? | Emacs takes many launch options. The one that you are looking for is
`emacs -nw`. This will open Emacs inside the terminal disregarding the *DISPLAY* environment variable even if it is set.
The long form of this flag is `emacs --no-window-system`.
More information about Emacs launch options can be found in [the manual... |
1391853 | Removing duplicate values from a PowerShell array | 162 | 2009-09-08 03:41:59 | <p>How can I remove duplicates from a PowerShell array?</p>
<pre><code>$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
</code></pre>
| 324,749 | 3,957 | 2025-01-05 07:35:10 | 1,391,865 | 267 | 2009-09-08 03:46:28 | 153,982 | 2019-06-22 18:51:23 | https://stackoverflow.com/q/1391853 | https://stackoverflow.com/a/1391865 | <p>Use <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object" rel="noreferrer"><code>Select-Object</code></a> (whose alias is <code>select</code>) with the <code>-Unique</code> switch; e.g.:</p>
<pre><code>$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
$a = $a | select -Unique
</code... | <p>Use <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object" rel="noreferrer"><code>Select-Object</code></a> (whose alias is <code>select</code>) with the <code>-Unique</code> switch; e.g.:</p> <pre><code>$a = @(1,2,3,4,5,5,6,7,8,9,0,0) $a = $a | select -Unique </code... | 114, 526 | arrays, powershell | <h1>Removing duplicate values from a PowerShell array</h1>
<p>How can I remove duplicates from a PowerShell array?</p>
<pre><code>$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 755 | bash | # Removing duplicate values from a PowerShell array
How can I remove duplicates from a PowerShell array?
```
$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
``` | Use [`Select-Object`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object) (whose alias is `select`) with the `-Unique` switch; e.g.:
```
$a = @(1,2,3,4,5,5,6,7,8,9,0,0)
$a = $a | select -Unique
``` |
7760013 | Why does 'continue' behave like 'break' in a Foreach-Object? | 193 | 2011-10-13 20:24:27 | <p>If I do the following in a PowerShell script:</p>
<pre><code>$range = 1..100
ForEach ($_ in $range) {
if ($_ % 7 -ne 0 ) { continue; }
Write-Host "$($_) is a multiple of 7"
}
</code></pre>
<p>I get the expected output of:</p>
<pre><code>7 is a multiple of 7
14 is a multiple of 7
21 is a multiple of 7
28 i... | 113,099 | 95,195 | 2019-10-01 16:40:22 | 7,763,698 | 265 | 2011-10-14 06:07:43 | 323,582 | 2019-10-01 16:22:51 | https://stackoverflow.com/q/7760013 | https://stackoverflow.com/a/7763698 | <p>Simply use the <code>return</code> instead of the <code>continue</code>. This <code>return</code> returns from the script block which is invoked by <code>ForEach-Object</code> on a particular iteration, thus, it simulates the <code>continue</code> in a loop.</p>
<pre><code>1..100 | ForEach-Object {
if ($_ % 7 -... | <p>Simply use the <code>return</code> instead of the <code>continue</code>. This <code>return</code> returns from the script block which is invoked by <code>ForEach-Object</code> on a particular iteration, thus, it simulates the <code>continue</code> in a loop.</p> <pre><code>1..100 | ForEach-Object { if ($_ % 7 -... | 526, 631 | foreach, powershell | <h1>Why does 'continue' behave like 'break' in a Foreach-Object?</h1>
<p>If I do the following in a PowerShell script:</p>
<pre><code>$range = 1..100
ForEach ($_ in $range) {
if ($_ % 7 -ne 0 ) { continue; }
Write-Host "$($_) is a multiple of 7"
}
</code></pre>
<p>I get the expected output of:</p>
<pre><code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 756 | bash | # Why does 'continue' behave like 'break' in a Foreach-Object?
If I do the following in a PowerShell script:
```
$range = 1..100
ForEach ($_ in $range) {
if ($_ % 7 -ne 0 ) { continue; }
Write-Host "$($_) is a multiple of 7"
}
```
I get the expected output of:
```
7 is a multiple of 7
14 is a multiple of 7
... | Simply use the `return` instead of the `continue`. This `return` returns from the script block which is invoked by `ForEach-Object` on a particular iteration, thus, it simulates the `continue` in a loop.
```
1..100 | ForEach-Object {
if ($_ % 7 -ne 0 ) { return }
Write-Host "$($_) is a multiple of 7"
}
```
Th... |
11526285 | How to count objects in PowerShell? | 192 | 2012-07-17 15:52:28 | <p>As I'm reading in the PowerShell user guide, one of the core PowerShell concepts is that commands accept and return <em>objects</em> instead of text. So for example, running <code>get-alias</code> returns me a number of <code>System.Management.Automation.AliasInfo</code> objects:</p>
<pre>
PS Z:\> get-alias
Comman... | 607,156 | 531,179 | 2023-02-02 15:08:58 | 11,526,317 | 265 | 2012-07-17 15:54:29 | 411,691 | 2012-07-17 16:05:05 | https://stackoverflow.com/q/11526285 | https://stackoverflow.com/a/11526317 | <p>This will get you count:</p>
<pre><code>get-alias | measure
</code></pre>
<p>You can work with the result as with object:</p>
<pre><code>$m = get-alias | measure
$m.Count
</code></pre>
<p>And if you would like to have aliases in some variable also, you can use Tee-Object:</p>
<pre><code>$m = get-alias | tee -Va... | <p>This will get you count:</p> <pre><code>get-alias | measure </code></pre> <p>You can work with the result as with object:</p> <pre><code>$m = get-alias | measure $m.Count </code></pre> <p>And if you would like to have aliases in some variable also, you can use Tee-Object:</p> <pre><code>$m = get-alias | tee -Va... | 526, 531 | powershell, scripting | <h1>How to count objects in PowerShell?</h1>
<p>As I'm reading in the PowerShell user guide, one of the core PowerShell concepts is that commands accept and return <em>objects</em> instead of text. So for example, running <code>get-alias</code> returns me a number of <code>System.Management.Automation.AliasInfo</code> ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 757 | bash | # How to count objects in PowerShell?
As I'm reading in the PowerShell user guide, one of the core PowerShell concepts is that commands accept and return *objects* instead of text. So for example, running `get-alias` returns me a number of `System.Management.Automation.AliasInfo` objects:
```
PS Z:\> get-alias
Comma... | This will get you count:
```
get-alias | measure
```
You can work with the result as with object:
```
$m = get-alias | measure
$m.Count
```
And if you would like to have aliases in some variable also, you can use Tee-Object:
```
$m = get-alias | tee -Variable aliases | measure
$m.Count
$aliases
```
Some more info... |
5131948 | Use of an exclamation mark in a Git commit message via the command line | 184 | 2011-02-27 07:13:39 | <p>How do I enter an exclamation point into a Git commit message from the command line?</p>
<p>It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.</p>
<p>I want something like this:</p>
<pre><code>git commit -am "Nailed it!"
</code></pre>
| 24,531 | 68,210 | 2019-08-27 13:45:15 | 5,131,960 | 265 | 2011-02-27 07:16:37 | 207,248 | 2011-02-27 07:25:17 | https://stackoverflow.com/q/5131948 | https://stackoverflow.com/a/5131960 | <p>Use single quotes instead of double quotes</p>
<pre><code>git commit -am 'Nailed it!'
</code></pre>
<p>Alternatively, if you need to use double quotes for whatever reason but still want a literal <code>!</code> then turn off history expansion at the top of your script via <code>set +H</code></p>
| <p>Use single quotes instead of double quotes</p> <pre><code>git commit -am 'Nailed it!' </code></pre> <p>Alternatively, if you need to use double quotes for whatever reason but still want a literal <code>!</code> then turn off history expansion at the top of your script via <code>set +H</code></p> | 119, 387 | bash, git | <h1>Use of an exclamation mark in a Git commit message via the command line</h1>
<p>How do I enter an exclamation point into a Git commit message from the command line?</p>
<p>It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.</p>
<p>I want s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 758 | bash | # Use of an exclamation mark in a Git commit message via the command line
How do I enter an exclamation point into a Git commit message from the command line?
It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.
I want something like this:
``... | Use single quotes instead of double quotes
```
git commit -am 'Nailed it!'
```
Alternatively, if you need to use double quotes for whatever reason but still want a literal `!` then turn off history expansion at the top of your script via `set +H` |
10823635 | How to include file in a bash shell script | 182 | 2012-05-30 20:19:02 | <p>Is there a way to include another shell script in a shell script to be able to access its functions?</p>
<p>Like how in PHP you can use the <code>include</code> directive with other PHP files in order to run the functions that are contained within simply by calling the function name.</p>
| 265,782 | 781,339 | 2024-02-22 17:41:55 | 10,823,650 | 265 | 2012-05-30 20:20:14 | 465,183 | 2020-07-13 23:00:29 | https://stackoverflow.com/q/10823635 | https://stackoverflow.com/a/10823650 | <p>Simply put inside your script :</p>
<pre><code>source FILE
</code></pre>
<p>Or</p>
<pre><code>. FILE # POSIX compliant
</code></pre>
<br>
<pre><code>$ LANG=C help source
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shel... | <p>Simply put inside your script :</p> <pre><code>source FILE </code></pre> <p>Or</p> <pre><code>. FILE # POSIX compliant </code></pre> <br> <pre><code>$ LANG=C help source source: source filename [arguments] Execute commands from a file in the current shell. Read and execute commands from FILENAME in the current shel... | 58, 387, 10402 | bash, include, linux | <h1>How to include file in a bash shell script</h1>
<p>Is there a way to include another shell script in a shell script to be able to access its functions?</p>
<p>Like how in PHP you can use the <code>include</code> directive with other PHP files in order to run the functions that are contained within simply by callin... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 759 | bash | # How to include file in a bash shell script
Is there a way to include another shell script in a shell script to be able to access its functions?
Like how in PHP you can use the `include` directive with other PHP files in order to run the functions that are contained within simply by calling the function name. | Simply put inside your script :
```
source FILE
```
Or
```
. FILE # POSIX compliant
```
```
$ LANG=C help source
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the direct... |
18413014 | Run a JAR file from the command line and specify classpath | 160 | 2013-08-23 22:50:19 | <p>I've compiled a <a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29" rel="noreferrer">JAR</a> file and specified the Main-Class in the manifest (I used the Eclipse <em>Export</em> function). My dependencies are all in a directory labeled <code>lib</code>. I can't seem to get a straight answer on how to execu... | 329,973 | 2,161,954 | 2019-12-17 21:20:55 | 18,413,058 | 265 | 2013-08-23 22:54:28 | null | 2013-08-23 22:54:28 | https://stackoverflow.com/q/18413014 | https://stackoverflow.com/a/18413058 | <p>When you specify <code>-jar</code> then the <code>-cp</code> parameter will be ignored. </p>
<p>From <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html#jar">the documentation</a>:</p>
<blockquote>
<p><i>When you use this option, the JAR file is the source of all user classes, and oth... | <p>When you specify <code>-jar</code> then the <code>-cp</code> parameter will be ignored. </p> <p>From <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html#jar">the documentation</a>:</p> <blockquote> <p><i>When you use this option, the JAR file is the source of all user classes, and oth... | 17, 390, 1231, 8005 | classpath, command-line, java, shell | <h1>Run a JAR file from the command line and specify classpath</h1>
<p>I've compiled a <a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29" rel="noreferrer">JAR</a> file and specified the Main-Class in the manifest (I used the Eclipse <em>Export</em> function). My dependencies are all in a directory labeled <co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 760 | bash | # Run a JAR file from the command line and specify classpath
I've compiled a [JAR](http://en.wikipedia.org/wiki/JAR_%28file_format%29) file and specified the Main-Class in the manifest (I used the Eclipse *Export* function). My dependencies are all in a directory labeled `lib`. I can't seem to get a straight answer on... | When you specify `-jar` then the `-cp` parameter will be ignored.
From [the documentation](http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/java.html#jar):
> *When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.*
You also cannot "includ... |
11904772 | How to create a loop in bash that is waiting for a webserver to respond? | 159 | 2012-08-10 15:26:43 | <p>How to create a loop in bash that is waiting for a webserver to respond?</p>
<p>It should print a "." every 10 seconds or so, and wait until the server starts to respond.</p>
<p>Update, this code tests if I get a good response from the server.</p>
<pre>
if curl --output /dev/null --silent --head --fail "$url"; th... | 142,933 | 99,834 | 2024-03-21 07:39:15 | 21,189,440 | 265 | 2014-01-17 15:22:12 | 266,875 | 2024-03-21 07:39:15 | https://stackoverflow.com/q/11904772 | https://stackoverflow.com/a/21189440 | <p>Combining the question with chepner's answer, this worked for me:</p>
<pre class="lang-bash prettyprint-override"><code>until curl --output /dev/null --silent --head --fail http://myhost:myport; do
printf '.'
sleep 5
done
</code></pre>
| <p>Combining the question with chepner's answer, this worked for me:</p> <pre class="lang-bash prettyprint-override"><code>until curl --output /dev/null --silent --head --fail http://myhost:myport; do printf '.' sleep 5 done </code></pre> | 387 | bash | <h1>How to create a loop in bash that is waiting for a webserver to respond?</h1>
<p>How to create a loop in bash that is waiting for a webserver to respond?</p>
<p>It should print a "." every 10 seconds or so, and wait until the server starts to respond.</p>
<p>Update, this code tests if I get a good response from t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 761 | bash | # How to create a loop in bash that is waiting for a webserver to respond?
How to create a loop in bash that is waiting for a webserver to respond?
It should print a "." every 10 seconds or so, and wait until the server starts to respond.
Update, this code tests if I get a good response from the server.
```
if curl... | Combining the question with chepner's answer, this worked for me:
```
until curl --output /dev/null --silent --head --fail http://myhost:myport; do
printf '.'
sleep 5
done
``` |
35813186 | Extract the filename from a path | 152 | 2016-03-05 10:51:39 | <p>I want to extract filename from below path:</p>
<pre>D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv</pre>
<p>Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to rewrite. I looking a way ... | 234,090 | 664,481 | 2024-12-11 03:12:39 | 35,813,307 | 265 | 2016-03-05 11:06:19 | 4,552,490 | 2016-03-05 11:12:08 | https://stackoverflow.com/q/35813186 | https://stackoverflow.com/a/35813307 | <p>If you are ok with including the extension this should do what you want.</p>
<pre><code>$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$outputFile = Split-Path $outputPath -leaf
</code></pre>
| <p>If you are ok with including the extension this should do what you want.</p> <pre><code>$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv" $outputFile = Split-Path $outputPath -leaf </code></pre> | 526, 24067 | powershell, powershell-2.0 | <h1>Extract the filename from a path</h1>
<p>I want to extract filename from below path:</p>
<pre>D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv</pre>
<p>Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 762 | bash | # Extract the filename from a path
I want to extract filename from below path:
```
D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv
```
Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to ... | If you are ok with including the extension this should do what you want.
```
$outputPath = "D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv"
$outputFile = Split-Path $outputPath -leaf
``` |
13676457 | How can I put the current running linux process in background? | 147 | 2012-12-03 02:41:12 | <p>I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish.</p>
<p>How can I put that running program in background? So that I can still work on shell and that process also gets completed?</p>
| 216,196 | 767,244 | 2020-03-15 07:40:41 | 13,676,865 | 265 | 2012-12-03 03:47:53 | 892,256 | 2020-03-15 07:40:41 | https://stackoverflow.com/q/13676457 | https://stackoverflow.com/a/13676865 | <p>Suspend the process with CTRL+Z then use the command <code>bg</code> to resume it in background. For example:</p>
<pre><code>sleep 60
^Z #Suspend character shown after hitting CTRL+Z
[1]+ Stopped sleep 60 #Message showing stopped process info
bg #Resume current job (last job stopped)
</code></pre>
<p>More about... | <p>Suspend the process with CTRL+Z then use the command <code>bg</code> to resume it in background. For example:</p> <pre><code>sleep 60 ^Z #Suspend character shown after hitting CTRL+Z [1]+ Stopped sleep 60 #Message showing stopped process info bg #Resume current job (last job stopped) </code></pre> <p>More about... | 58, 387, 390, 2827 | background, bash, linux, shell | <h1>How can I put the current running linux process in background?</h1>
<p>I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish.</p>
<p>How can I put that running program in background? So that I can still work on shell and that process also gets ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 763 | bash | # How can I put the current running linux process in background?
I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish.
How can I put that running program in background? So that I can still work on shell and that process also gets completed? | Suspend the process with CTRL+Z then use the command `bg` to resume it in background. For example:
```
sleep 60
^Z #Suspend character shown after hitting CTRL+Z
[1]+ Stopped sleep 60 #Message showing stopped process info
bg #Resume current job (last job stopped)
```
More about job control and `bg` usage in `bash... |
6901171 | Is \d not supported by grep's basic expressions? | 163 | 2011-08-01 16:04:47 | <p>This does not generate any output. How come?</p>
<pre><code>$ echo 'this 1 2 3' | grep '\d\+'
</code></pre>
<p>But these do:</p>
<pre><code>$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3
$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3
</code></pre>
| 61,456 | 494,074 | 2026-01-19 18:44:01 | 6,901,221 | 264 | 2011-08-01 16:08:28 | 350,351 | 2026-01-19 18:44:01 | https://stackoverflow.com/q/6901171 | https://stackoverflow.com/a/6901221 | <p>As specified in POSIX, <code>grep</code> uses <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html" rel="nofollow noreferrer">basic regular expressions</a>, but <code>\d</code> is part of a Perl-compatible regular expression (<a href="https://www.pcre.org/current/doc/html/pcre2pattern.ht... | <p>As specified in POSIX, <code>grep</code> uses <a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html" rel="nofollow noreferrer">basic regular expressions</a>, but <code>\d</code> is part of a Perl-compatible regular expression (<a href="https://www.pcre.org/current/doc/html/pcre2pattern.ht... | 58, 387, 1271 | bash, grep, linux | <h1>Is \d not supported by grep's basic expressions?</h1>
<p>This does not generate any output. How come?</p>
<pre><code>$ echo 'this 1 2 3' | grep '\d\+'
</code></pre>
<p>But these do:</p>
<pre><code>$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3
$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 764 | bash | # Is \d not supported by grep's basic expressions?
This does not generate any output. How come?
```
$ echo 'this 1 2 3' | grep '\d\+'
```
But these do:
```
$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3
$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3
``` | As specified in POSIX, `grep` uses [basic regular expressions](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html), but `\d` is part of a Perl-compatible regular expression ([PCRE](https://www.pcre.org/current/doc/html/pcre2pattern.html#genericchartypes)).
If you are using GNU grep, you can use t... |
13460482 | Exclude a sub-directory using find | 153 | 2012-11-19 19:11:28 | <p>I have directory structure like this</p>
<pre><code>data
|___
|
abc
|____incoming
def
|____incoming
|____processed
123
|___incoming
456
|___incoming
|___processed
</code></pre>
<p>There is an incoming sub-folder in all of the folders inside Data directory. I want to get all f... | 126,643 | 177,971 | 2023-07-19 17:01:13 | 13,460,641 | 263 | 2012-11-19 19:22:27 | 1,741,864 | 2012-11-19 19:53:55 | https://stackoverflow.com/q/13460482 | https://stackoverflow.com/a/13460641 | <p>This works:</p>
<pre><code>find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"
</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>find /home/feeds/data</code>: start finding recursively from specified path</li>
<li><code>-type f</code>: find files only</li>
<li><co... | <p>This works:</p> <pre><code>find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*" </code></pre> <p><strong>Explanation:</strong></p> <ul> <li><code>find /home/feeds/data</code>: start finding recursively from specified path</li> <li><code>-type f</code>: find files only</li> <li><co... | 34, 387 | bash, unix | <h1>Exclude a sub-directory using find</h1>
<p>I have directory structure like this</p>
<pre><code>data
|___
|
abc
|____incoming
def
|____incoming
|____processed
123
|___incoming
456
|___incoming
|___processed
</code></pre>
<p>There is an incoming sub-folder in all of the folder... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 765 | bash | # Exclude a sub-directory using find
I have directory structure like this
```
data
|___
|
abc
|____incoming
def
|____incoming
|____processed
123
|___incoming
456
|___incoming
|___processed
```
There is an incoming sub-folder in all of the folders inside Data directory. I want t... | This works:
```
find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"
```
**Explanation:**
- `find /home/feeds/data`: start finding recursively from specified path
- `-type f`: find files only
- `-not -path "*def/incoming*"`: don't include anything with `def/incoming` as part of its p... |
13525004 | How to exclude this / current / dot folder from find "type d" | 285 | 2012-11-23 07:55:10 | <pre><code>find . -type d
</code></pre>
<p>can be used to find all directories below some start point. But it returns the current directory (<code>.</code>) too, which may be undesired. How can it be excluded?</p>
| 132,950 | 1,503,237 | 2022-02-24 09:20:08 | 17,389,439 | 262 | 2013-06-30 10:32:03 | 895,245 | 2016-03-11 21:09:23 | https://stackoverflow.com/q/13525004 | https://stackoverflow.com/a/17389439 | <p><a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html#tag_20_47" rel="noreferrer"><strong>POSIX 7 solution</strong></a>:</p>
<pre><code>find . ! -path . -type d
</code></pre>
<p>For this particular case (<code>.</code>), golfs better than the <code>mindepth</code> solution (24 vs 26 chars), ... | <p><a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html#tag_20_47" rel="noreferrer"><strong>POSIX 7 solution</strong></a>:</p> <pre><code>find . ! -path . -type d </code></pre> <p>For this particular case (<code>.</code>), golfs better than the <code>mindepth</code> solution (24 vs 26 chars), ... | 218, 390, 10193 | directory, find, shell | <h1>How to exclude this / current / dot folder from find "type d"</h1>
<pre><code>find . -type d
</code></pre>
<p>can be used to find all directories below some start point. But it returns the current directory (<code>.</code>) too, which may be undesired. How can it be excluded?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 766 | bash | # How to exclude this / current / dot folder from find "type d"
```
find . -type d
```
can be used to find all directories below some start point. But it returns the current directory (`.`) too, which may be undesired. How can it be excluded? | [**POSIX 7 solution**](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html#tag_20_47):
```
find . ! -path . -type d
```
For this particular case (`.`), golfs better than the `mindepth` solution (24 vs 26 chars), although this is probably slightly harder to type because of the `!`.
To exclude other di... |
12230690 | String comparison in bash. [[: not found | 235 | 2012-09-01 19:23:56 | <p>I am trying to compare strings in bash. I already found an answer on how to do it on <a href="https://stackoverflow.com/questions/229551/string-contains-in-bash">stackoverflow</a>. In script I am trying, I am using the code submitted by Adam in the mentioned question:</p>
<pre><code>#!/bin/bash
string='My string';
... | 225,177 | 1,581,900 | 2023-07-04 09:42:44 | 12,230,723 | 262 | 2012-09-01 19:27:45 | 1,630,171 | 2020-07-23 06:31:30 | https://stackoverflow.com/q/12230690 | https://stackoverflow.com/a/12230723 | <p><code>[[</code> is a bash-builtin. Your <code>/bin/bash</code> doesn't seem to be an actual bash.</p>
<p><em>From a comment:</em></p>
<p>Add <code>#!/bin/bash</code> at the top of file</p>
| <p><code>[[</code> is a bash-builtin. Your <code>/bin/bash</code> doesn't seem to be an actual bash.</p> <p><em>From a comment:</em></p> <p>Add <code>#!/bin/bash</code> at the top of file</p> | 387, 390, 16493, 67982 | bash, shell, string-comparison, ubuntu-11.04 | <h1>String comparison in bash. [[: not found</h1>
<p>I am trying to compare strings in bash. I already found an answer on how to do it on <a href="https://stackoverflow.com/questions/229551/string-contains-in-bash">stackoverflow</a>. In script I am trying, I am using the code submitted by Adam in the mentioned question... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 767 | bash | # String comparison in bash. [[: not found
I am trying to compare strings in bash. I already found an answer on how to do it on [stackoverflow](https://stackoverflow.com/questions/229551/string-contains-in-bash). In script I am trying, I am using the code submitted by Adam in the mentioned question:
```
#!/bin/bash
s... | `[[` is a bash-builtin. Your `/bin/bash` doesn't seem to be an actual bash.
*From a comment:*
Add `#!/bin/bash` at the top of file |
21063765 | Why is $$ returning the same id as the parent process? | 195 | 2014-01-11 14:57:45 | <p>I have problem with Bash, and I don't know why.<br />
Under shell, I enter:</p>
<pre><code>echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
</code></pre>
<p>Where <code>getpid</code> is a C program to get current <em>pid</em>, like:</p>
<blockquote>
<pre><code> int main() {
print... | 333,872 | 1,267,982 | 2022-10-08 13:11:05 | 21,063,837 | 262 | 2014-01-11 15:04:05 | 1,126,841 | 2014-01-11 15:04:05 | https://stackoverflow.com/q/21063765 | https://stackoverflow.com/a/21063837 | <p><code>$$</code> is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":</p>
<blockquote>
<p>$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell. </p>
</blockquote>
<p>In <code>... | <p><code>$$</code> is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":</p> <blockquote> <p>$ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell. </p> </blockquote> <p>In <code>... | 387, 390, 3300, 57788 | bash, pid, shell, subshell | <h1>Why is $$ returning the same id as the parent process?</h1>
<p>I have problem with Bash, and I don't know why.<br />
Under shell, I enter:</p>
<pre><code>echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
</code></pre>
<p>Where <code>getpid</code> is a C program to get current <em>pid</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 768 | bash | # Why is $$ returning the same id as the parent process?
I have problem with Bash, and I don't know why.
Under shell, I enter:
```
echo $$ ## print 2433
(echo $$) ## also print 2433
(./getpid) ## print 2602
```
Where `getpid` is a C program to get current *pid*, like:
> ```
> int main() {
> printf("%d"... | `$$` is defined to return the process ID of the parent in a subshell; from the man page under "Special Parameters":
> $ Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the current shell, not the subshell.
In `bash` 4, you can get the process ID of the child with `BASHPID`.
`... |
1145704 | How can you use an object's property in a double-quoted string? | 169 | 2009-07-17 21:11:45 | <p>I have the following code:</p>
<pre><code>$DatabaseSettings = @();
$NewDatabaseSetting = "" | select DatabaseName, DataFile, LogFile, LiveBackupPath;
$NewDatabaseSetting.DatabaseName = "LiveEmployees_PD";
$NewDatabaseSetting.DataFile = "LiveEmployees_PD_Data";
$NewDatabaseSetting.LogFile = "LiveEmployees_PD_Log";
$... | 113,624 | 90,689 | 2021-11-27 20:14:16 | 1,145,822 | 262 | 2009-07-17 21:41:57 | 73,070 | 2019-06-18 11:15:57 | https://stackoverflow.com/q/1145704 | https://stackoverflow.com/a/1145822 | <p>When you enclose a variable name in a double-quoted string it will be replaced by that variable's value:</p>
<pre><code>$foo = 2
"$foo"
</code></pre>
<p>becomes</p>
<pre><code>"2"
</code></pre>
<p>If you don't want that you have to use single quotes:</p>
<pre><code>$foo = 2
'$foo'
</code></pre>
<p>However, if ... | <p>When you enclose a variable name in a double-quoted string it will be replaced by that variable's value:</p> <pre><code>$foo = 2 "$foo" </code></pre> <p>becomes</p> <pre><code>"2" </code></pre> <p>If you don't want that you have to use single quotes:</p> <pre><code>$foo = 2 '$foo' </code></pre> <p>However, if ... | 526, 25968 | powershell, string-interpolation | <h1>How can you use an object's property in a double-quoted string?</h1>
<p>I have the following code:</p>
<pre><code>$DatabaseSettings = @();
$NewDatabaseSetting = "" | select DatabaseName, DataFile, LogFile, LiveBackupPath;
$NewDatabaseSetting.DatabaseName = "LiveEmployees_PD";
$NewDatabaseSetting.DataFile = "LiveEm... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 769 | bash | # How can you use an object's property in a double-quoted string?
I have the following code:
```
$DatabaseSettings = @();
$NewDatabaseSetting = "" | select DatabaseName, DataFile, LogFile, LiveBackupPath;
$NewDatabaseSetting.DatabaseName = "LiveEmployees_PD";
$NewDatabaseSetting.DataFile = "LiveEmployees_PD_Data";
$N... | When you enclose a variable name in a double-quoted string it will be replaced by that variable's value:
```
$foo = 2
"$foo"
```
becomes
```
"2"
```
If you don't want that you have to use single quotes:
```
$foo = 2
'$foo'
```
However, if you want to access properties, or use indexes on variables in a double-quot... |
9139401 | Trying to embed newline in a variable in Bash | 158 | 2012-02-04 08:03:52 | <p>I have</p>
<pre><code>var="a b c"
for i in $var
do
p=`echo -e $p'\n'$i`
done
echo $p
</code></pre>
<p>I want the last <em>echo</em> to print:</p>
<pre><code>a
b
c
</code></pre>
<p>Notice that I want the variable <em>p</em> to contain newlines. How do I do that?</p>
| 290,571 | 494,074 | 2022-09-26 23:42:59 | 9,139,891 | 262 | 2012-02-04 09:40:44 | 938,111 | 2022-09-26 23:42:59 | https://stackoverflow.com/q/9139401 | https://stackoverflow.com/a/9139891 | <h1>Summary</h1>
<ol>
<li><p>Inserting a new line in the source code</p>
<pre><code> p="${var1}
${var2}"
echo "${p}"
</code></pre>
</li>
<li><p>Using <code>$'\n'</code> (only <a href="https://stackoverflow.com/questions/tagged/bash">Bash</a> and <a href="https://stackoverflow.com/questions/tagged/... | <h1>Summary</h1> <ol> <li><p>Inserting a new line in the source code</p> <pre><code> p="${var1} ${var2}" echo "${p}" </code></pre> </li> <li><p>Using <code>$'\n'</code> (only <a href="https://stackoverflow.com/questions/tagged/bash">Bash</a> and <a href="https://stackoverflow.com/questions/tagged/... | 387 | bash | <h1>Trying to embed newline in a variable in Bash</h1>
<p>I have</p>
<pre><code>var="a b c"
for i in $var
do
p=`echo -e $p'\n'$i`
done
echo $p
</code></pre>
<p>I want the last <em>echo</em> to print:</p>
<pre><code>a
b
c
</code></pre>
<p>Notice that I want the variable <em>p</em> to contain newlines. How d... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 770 | bash | # Trying to embed newline in a variable in Bash
I have
```
var="a b c"
for i in $var
do
p=`echo -e $p'\n'$i`
done
echo $p
```
I want the last *echo* to print:
```
a
b
c
```
Notice that I want the variable *p* to contain newlines. How do I do that? | # Summary
1. Inserting a new line in the source code
```
p="${var1}
${var2}"
echo "${p}"
```
2. Using `$'\n'` (only [Bash](https://stackoverflow.com/questions/tagged/bash) and [Z shell](https://stackoverflow.com/questions/tagged/zsh))
```
p="${var1}"$'\n'"${var2}"
echo "${p}"
```
3. U... |
17804007 | How to show line number when executing bash script | 141 | 2013-07-23 07:31:23 | <p>I have a test script which has a lot of commands and will generate lots of output, I use <code>set -x</code> or <code>set -v</code> and <code>set -e</code>, so the script would stop when error occurs. However, it's still rather difficult for me to locate which line did the execution stop in order to locate the probl... | 123,134 | 2,087,906 | 2024-10-15 10:15:30 | 17,805,088 | 262 | 2013-07-23 08:28:37 | 2,235,132 | 2024-02-02 00:39:34 | https://stackoverflow.com/q/17804007 | https://stackoverflow.com/a/17805088 | <p>You mention that you're already using <code>-x</code>. The variable <code>PS4</code> denotes the value is the prompt printed before the command line is echoed when the <code>-x</code> option is set and defaults to <code>:</code> followed by space.</p>
<p>You can change <code>PS4</code> to emit the <code>LINENO</cod... | <p>You mention that you're already using <code>-x</code>. The variable <code>PS4</code> denotes the value is the prompt printed before the command line is echoed when the <code>-x</code> option is set and defaults to <code>:</code> followed by space.</p> <p>You can change <code>PS4</code> to emit the <code>LINENO</cod... | 58, 186, 387 | bash, linux, testing | <h1>How to show line number when executing bash script</h1>
<p>I have a test script which has a lot of commands and will generate lots of output, I use <code>set -x</code> or <code>set -v</code> and <code>set -e</code>, so the script would stop when error occurs. However, it's still rather difficult for me to locate wh... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 771 | bash | # How to show line number when executing bash script
I have a test script which has a lot of commands and will generate lots of output, I use `set -x` or `set -v` and `set -e`, so the script would stop when error occurs. However, it's still rather difficult for me to locate which line did the execution stop in order t... | You mention that you're already using `-x`. The variable `PS4` denotes the value is the prompt printed before the command line is echoed when the `-x` option is set and defaults to `:` followed by space.
You can change `PS4` to emit the `LINENO` (The line number in the script or shell function currently executing).
F... |
22190902 | cut or awk command to print first field of first row | 113 | 2014-03-05 07:05:31 | <p>I am trying print the first field of the first row of an output. Here is the case. I just need to print only <code>SUSE</code> from this output. </p>
<pre><code># cat /etc/*release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
</code></pre>
<p>Tried with <code>cat /etc/*release | awk {'prin... | 322,035 | 3,331,975 | 2022-07-21 19:39:26 | 22,190,928 | 262 | 2014-03-05 07:07:04 | 2,235,132 | 2019-04-12 16:19:33 | https://stackoverflow.com/q/22190902 | https://stackoverflow.com/a/22190928 | <p>Specify <a href="https://www.gnu.org/software/gawk/manual/html_node/Auto_002dset.html#index-NR-variable-1" rel="noreferrer"><code>NR</code></a> if you want to capture output from selected rows:</p>
<pre><code>awk 'NR==1{print $1}' /etc/*release
</code></pre>
<p>An alternative (<em>ugly</em>) way of achieving the s... | <p>Specify <a href="https://www.gnu.org/software/gawk/manual/html_node/Auto_002dset.html#index-NR-variable-1" rel="noreferrer"><code>NR</code></a> if you want to capture output from selected rows:</p> <pre><code>awk 'NR==1{print $1}' /etc/*release </code></pre> <p>An alternative (<em>ugly</em>) way of achieving the s... | 34, 58, 387, 990 | awk, bash, linux, unix | <h1>cut or awk command to print first field of first row</h1>
<p>I am trying print the first field of the first row of an output. Here is the case. I just need to print only <code>SUSE</code> from this output. </p>
<pre><code># cat /etc/*release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
</c... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 772 | bash | # cut or awk command to print first field of first row
I am trying print the first field of the first row of an output. Here is the case. I just need to print only `SUSE` from this output.
```
# cat /etc/*release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
```
Tried with `cat /etc/*release ... | Specify [`NR`](https://www.gnu.org/software/gawk/manual/html_node/Auto_002dset.html#index-NR-variable-1) if you want to capture output from selected rows:
```
awk 'NR==1{print $1}' /etc/*release
```
An alternative (*ugly*) way of achieving the same would be:
```
awk '{print $1; exit}'
```
An efficient way of gettin... |
2869669 | In Bash, how do I add a string after each line in a file? | 170 | 2010-05-19 21:45:43 | <p>How do I add a string after each line in a file using bash? Can it be done using the sed command, if so how?</p>
| 254,197 | 345,537 | 2022-05-11 10:11:05 | 2,869,736 | 261 | 2010-05-19 21:54:58 | 335,909 | 2014-10-17 01:55:37 | https://stackoverflow.com/q/2869669 | https://stackoverflow.com/a/2869736 | <p>If your <code>sed</code> allows in place editing via the <code>-i</code> parameter:</p>
<pre><code>sed -e 's/$/string after each line/' -i filename
</code></pre>
<p>If not, you have to make a temporary file:</p>
<pre><code>typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/... | <p>If your <code>sed</code> allows in place editing via the <code>-i</code> parameter:</p> <pre><code>sed -e 's/$/string after each line/' -i filename </code></pre> <p>If not, you have to make a temporary file:</p> <pre><code>typeset TMP_FILE=$( mktemp ) touch "${TMP_FILE}" cp -p filename "${TMP_FILE}" sed -e 's/$/... | 34, 58, 387, 5282 | bash, linux, sed, unix | <h1>In Bash, how do I add a string after each line in a file?</h1>
<p>How do I add a string after each line in a file using bash? Can it be done using the sed command, if so how?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 773 | bash | # In Bash, how do I add a string after each line in a file?
How do I add a string after each line in a file using bash? Can it be done using the sed command, if so how? | If your `sed` allows in place editing via the `-i` parameter:
```
sed -e 's/$/string after each line/' -i filename
```
If not, you have to make a temporary file:
```
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
``` |
11201659 | What's the Android ADB shell "dumpsys" tool and what are its benefits? | 155 | 2012-06-26 06:14:48 | <p>I'm looking for the full list of ADB shell <code>dumpsys</code> commands with a full explanation of all of the commands.</p>
<p>Where can I find this information?</p>
| 245,423 | 587,012 | 2021-06-18 11:08:53 | 11,201,711 | 261 | 2012-06-26 06:19:44 | 1,327,899 | 2015-09-16 21:07:00 | https://stackoverflow.com/q/11201659 | https://stackoverflow.com/a/11201711 | <p><strong>What's dumpsys and what are its benefit</strong></p>
<p><em>dumpsys</em> is an android tool that runs on the device and dumps interesting information about the status of system services.</p>
<p>Obvious benefits:</p>
<ol>
<li>Possibility to easily get system information in a simple string representation.</... | <p><strong>What's dumpsys and what are its benefit</strong></p> <p><em>dumpsys</em> is an android tool that runs on the device and dumps interesting information about the status of system services.</p> <p>Obvious benefits:</p> <ol> <li>Possibility to easily get system information in a simple string representation.</... | 390, 1386, 22975 | adb, android, shell | <h1>What's the Android ADB shell "dumpsys" tool and what are its benefits?</h1>
<p>I'm looking for the full list of ADB shell <code>dumpsys</code> commands with a full explanation of all of the commands.</p>
<p>Where can I find this information?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 774 | bash | # What's the Android ADB shell "dumpsys" tool and what are its benefits?
I'm looking for the full list of ADB shell `dumpsys` commands with a full explanation of all of the commands.
Where can I find this information? | **What's dumpsys and what are its benefit**
*dumpsys* is an android tool that runs on the device and dumps interesting information about the status of system services.
Obvious benefits:
1. Possibility to easily get system information in a simple string representation.
2. Possibility to use dumped CPU, RAM, Battery, ... |
7451299 | How do I preserve the remote filename when Downloading a file using curl | 152 | 2011-09-16 23:19:51 | <blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/2698552/how-do-i-save-a-file-using-the-response-header-filename-with-curl">How do I save a file using the response header filename with cURL</a> </p>
</blockquote>
<p>I need to download many thousands of images... | 84,338 | 493,144 | 2018-03-29 18:55:15 | 7,451,779 | 261 | 2011-09-17 01:06:46 | 59,947 | 2018-03-29 18:55:15 | https://stackoverflow.com/q/7451299 | https://stackoverflow.com/a/7451779 | <p>The solution is to use <code>-O -J</code></p>
<blockquote>
<pre><code>-O, --remote-name Write output to a file named as the remote file
-J, --remote-header-name Use the header-provided filename
</code></pre>
</blockquote>
<p>So...</p>
<pre><code>curl -O -J 'http://oregondigital.org/cgi-bin/showfile... | <p>The solution is to use <code>-O -J</code></p> <blockquote> <pre><code>-O, --remote-name Write output to a file named as the remote file -J, --remote-header-name Use the header-provided filename </code></pre> </blockquote> <p>So...</p> <pre><code>curl -O -J 'http://oregondigital.org/cgi-bin/showfile... | 390, 531, 1554 | curl, scripting, shell | <h1>How do I preserve the remote filename when Downloading a file using curl</h1>
<blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/2698552/how-do-i-save-a-file-using-the-response-header-filename-with-curl">How do I save a file using the response header filename w... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 775 | bash | # How do I preserve the remote filename when Downloading a file using curl
> **Possible Duplicate:**
> [How do I save a file using the response header filename with cURL](https://stackoverflow.com/questions/2698552/how-do-i-save-a-file-using-the-response-header-filename-with-curl)
I need to download many thousands ... | The solution is to use `-O -J`
> ```
> -O, --remote-name Write output to a file named as the remote file
> -J, --remote-header-name Use the header-provided filename
> ```
So...
```
curl -O -J 'http://oregondigital.org/cgi-bin/showfile.exe?CISOROOT=/baseball&CISOPTR=0'
```
I had to upgrade my CURL. I ... |
15703664 | Find all zero-byte files in directory and subdirectories | 134 | 2013-03-29 12:54:33 | <p>How can I find all zero-byte files in a directory and its subdirectories?</p>
<p>I have done this:</p>
<pre><code>#!/bin/bash
lns=`vdir -R *.* $dir| awk '{print $8"\t"$5}'`
temp=""
for file in $lns; do
if test $file = "0"; then
printf $temp"\t"$file"\n"
fi
temp=$file
done
</code></pre>
<p>But,... | 201,908 | 2,134,440 | 2024-11-27 09:36:46 | 15,703,727 | 261 | 2013-03-29 12:58:35 | 140,750 | 2020-01-09 11:11:36 | https://stackoverflow.com/q/15703664 | https://stackoverflow.com/a/15703727 | <p>To print the names of all files in and below $dir of size 0:</p>
<pre><code>find "$dir" -size 0
</code></pre>
<p>Note that not all implementations of <code>find</code> will produce output by default, so you may need to do:</p>
<pre><code>find "$dir" -size 0 -print
</code></pre>
<p>Two comments on the final loop ... | <p>To print the names of all files in and below $dir of size 0:</p> <pre><code>find "$dir" -size 0 </code></pre> <p>Note that not all implementations of <code>find</code> will produce output by default, so you may need to do:</p> <pre><code>find "$dir" -size 0 -print </code></pre> <p>Two comments on the final loop ... | 58, 390 | linux, shell | <h1>Find all zero-byte files in directory and subdirectories</h1>
<p>How can I find all zero-byte files in a directory and its subdirectories?</p>
<p>I have done this:</p>
<pre><code>#!/bin/bash
lns=`vdir -R *.* $dir| awk '{print $8"\t"$5}'`
temp=""
for file in $lns; do
if test $file = "0"; then
printf $t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 776 | bash | # Find all zero-byte files in directory and subdirectories
How can I find all zero-byte files in a directory and its subdirectories?
I have done this:
```
#!/bin/bash
lns=`vdir -R *.* $dir| awk '{print $8"\t"$5}'`
temp=""
for file in $lns; do
if test $file = "0"; then
printf $temp"\t"$file"\n"
fi
... | To print the names of all files in and below $dir of size 0:
```
find "$dir" -size 0
```
Note that not all implementations of `find` will produce output by default, so you may need to do:
```
find "$dir" -size 0 -print
```
Two comments on the final loop in the question:
Rather than iterating over every other word ... |
1215260 | How to redirect the output of a PowerShell to a file during its execution | 289 | 2009-07-31 22:49:09 | <p>I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:</p>
<pre><code> .\MyScript.ps1 > output.txt
</code></pre>
<p>How do I redirect the output of a PowerShell script during its execution?</p>
| 840,033 | 42,024 | 2026-01-12 16:26:08 | 1,215,395 | 260 | 2009-07-31 23:51:17 | 14,326 | 2018-10-18 19:39:59 | https://stackoverflow.com/q/1215260 | https://stackoverflow.com/a/1215395 | <p>Maybe <code>Start-Transcript</code> would work for you. First stop it if it's already running, then start it, and stop it when done.</p>
<pre>
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-T... | <p>Maybe <code>Start-Transcript</code> would work for you. First stop it if it's already running, then start it, and stop it when done.</p> <pre> $ErrorActionPreference="SilentlyContinue" Stop-Transcript | out-null $ErrorActionPreference = "Continue" Start-Transcript -path C:\output.txt -append # Do some stuff Stop-T... | 526, 531, 4867 | powershell, scripting, stdout | <h1>How to redirect the output of a PowerShell to a file during its execution</h1>
<p>I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:</p>
<pre><code> .\MyScript.ps1 > output.txt
</code></pre>
<p>H... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 777 | bash | # How to redirect the output of a PowerShell to a file during its execution
I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:
```
.\MyScript.ps1 > output.txt
```
How do I redirect the output of a Pow... | Maybe `Start-Transcript` would work for you. First stop it if it's already running, then start it, and stop it when done.
```
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-Transcript
```
You ca... |
17282915 | How to download an entire directory and subdirectories using wget? | 184 | 2013-06-24 18:56:22 | <p>I am trying to download the files for a project using <code>wget</code>, as the SVN server for that project isn't running anymore and I am only able to access the files through a browser. The base URLs for all the files is the same like </p>
<blockquote>
<p><a href="http://abc.tamu.edu/projects/tzivi/repository/r... | 366,969 | 788,252 | 2025-10-20 21:48:38 | 19,695,143 | 260 | 2013-10-30 22:37:34 | null | 2017-09-27 09:41:35 | https://stackoverflow.com/q/17282915 | https://stackoverflow.com/a/19695143 | <p>You may use this in shell:</p>
<pre><code>wget -r --no-parent http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/
</code></pre>
<p>The Parameters are:</p>
<pre><code>-r //recursive Download
</code></pre>
<p>and </p>
<pre><code>--no-parent // Don´t download something from the parent directo... | <p>You may use this in shell:</p> <pre><code>wget -r --no-parent http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/ </code></pre> <p>The Parameters are:</p> <pre><code>-r //recursive Download </code></pre> <p>and </p> <pre><code>--no-parent // Don´t download something from the parent directo... | 18, 58, 387, 5583 | bash, linux, regex, wget | <h1>How to download an entire directory and subdirectories using wget?</h1>
<p>I am trying to download the files for a project using <code>wget</code>, as the SVN server for that project isn't running anymore and I am only able to access the files through a browser. The base URLs for all the files is the same like </p>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 778 | bash | # How to download an entire directory and subdirectories using wget?
I am trying to download the files for a project using `wget`, as the SVN server for that project isn't running anymore and I am only able to access the files through a browser. The base URLs for all the files is the same like
> <http://abc.tamu.edu/... | You may use this in shell:
```
wget -r --no-parent http://abc.tamu.edu/projects/tzivi/repository/revisions/2/raw/tzivi/
```
The Parameters are:
```
-r //recursive Download
```
and
```
--no-parent // Don´t download something from the parent directory
```
If you don't want to download the entire content, you ma... |
363223 | How do I get both STDOUT and STDERR to go to the terminal and a log file? | 175 | 2008-12-12 16:18:30 | <p>I have a script which will be run interactively by non-technical users. The script writes status updates to STDOUT so that the user can be sure that the script is running OK.</p>
<p>I want both STDOUT and STDERR redirected to the terminal (so that the user can see that the script is working as well as see if there ... | 131,856 | 1,019 | 2022-08-19 19:16:32 | 363,239 | 260 | 2008-12-12 16:21:29 | 3,333 | 2022-08-19 19:16:32 | https://stackoverflow.com/q/363223 | https://stackoverflow.com/a/363239 | <p>Use "tee" to redirect to a file and the screen. Depending on the shell you use, you first have to redirect stderr to stdout using</p>
<pre><code>./a.out 2>&1 | tee output
</code></pre>
<p>or</p>
<pre><code>./a.out |& tee output
</code></pre>
<p>In csh, there is a built-in command called "s... | <p>Use "tee" to redirect to a file and the screen. Depending on the shell you use, you first have to redirect stderr to stdout using</p> <pre><code>./a.out 2>&1 | tee output </code></pre> <p>or</p> <pre><code>./a.out |& tee output </code></pre> <p>In csh, there is a built-in command called "s... | 387, 390, 942 | bash, logging, shell | <h1>How do I get both STDOUT and STDERR to go to the terminal and a log file?</h1>
<p>I have a script which will be run interactively by non-technical users. The script writes status updates to STDOUT so that the user can be sure that the script is running OK.</p>
<p>I want both STDOUT and STDERR redirected to the ter... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 779 | bash | # How do I get both STDOUT and STDERR to go to the terminal and a log file?
I have a script which will be run interactively by non-technical users. The script writes status updates to STDOUT so that the user can be sure that the script is running OK.
I want both STDOUT and STDERR redirected to the terminal (so that t... | Use "tee" to redirect to a file and the screen. Depending on the shell you use, you first have to redirect stderr to stdout using
```
./a.out 2>&1 | tee output
```
or
```
./a.out |& tee output
```
In csh, there is a built-in command called "script" that will capture everything that goes to the screen to a file. You... |
8749929 | How do I concatenate two text files in PowerShell? | 157 | 2012-01-05 21:16:08 | <p>I am trying to replicate the functionality of the <code>cat</code> command in Unix.</p>
<p>I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable.</p>
| 224,270 | 391,161 | 2022-02-22 18:28:24 | 8,750,029 | 260 | 2012-01-05 21:23:42 | 1,128,737 | 2021-07-29 13:57:42 | https://stackoverflow.com/q/8749929 | https://stackoverflow.com/a/8750029 | <p>Simply use the <a href="https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Get-Content" rel="noreferrer"><code>Get-Content</code></a> and <a href="https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Set-Content" rel="noreferrer"><code>Set-Content</code></a> cmdlet... | <p>Simply use the <a href="https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Get-Content" rel="noreferrer"><code>Get-Content</code></a> and <a href="https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Set-Content" rel="noreferrer"><code>Set-Content</code></a> cmdlet... | 526, 717, 17938, 24067 | merge, powershell, powershell-2.0, text-files | <h1>How do I concatenate two text files in PowerShell?</h1>
<p>I am trying to replicate the functionality of the <code>cat</code> command in Unix.</p>
<p>I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable.<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 780 | bash | # How do I concatenate two text files in PowerShell?
I am trying to replicate the functionality of the `cat` command in Unix.
I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable. | Simply use the [`Get-Content`](https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Get-Content) and [`Set-Content`](https://learn.microsoft.com/powershell/module/Microsoft.PowerShell.Management/Set-Content) cmdlets:
```
Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt
`... |
1030182 | How do I change bash history completion to complete what's already on the line? | 150 | 2009-06-23 01:13:12 | <p>I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow: </p>
<pre><code>$ vim fi
</code></pre>
<p>Press <kbd>↑</kbd></p>
<pre><code>$ vim file.py
</code></pre>
<p>I'd like to set this up on my new computer, because it saves a lot ... | 63,406 | 125,843 | 2024-12-28 04:19:40 | 1,030,206 | 260 | 2009-06-23 01:24:42 | 20,713 | 2015-04-17 10:43:09 | https://stackoverflow.com/q/1030182 | https://stackoverflow.com/a/1030206 | <p>Probably something like</p>
<pre>
# ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
</pre>
<p>or equivalently,</p>
<pre>
# ~/.bashrc
if [[ $- == *i* ]]
then
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
fi
</pre>
<p>(the if statement checks for int... | <p>Probably something like</p> <pre> # ~/.inputrc "\e[A": history-search-backward "\e[B": history-search-forward </pre> <p>or equivalently,</p> <pre> # ~/.bashrc if [[ $- == *i* ]] then bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward' fi </pre> <p>(the if statement checks for int... | 58, 387, 390, 10353 | bash, linux, readline, shell | <h1>How do I change bash history completion to complete what's already on the line?</h1>
<p>I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow: </p>
<pre><code>$ vim fi
</code></pre>
<p>Press <kbd>↑</kbd></p>
<pre><code>$ vim file... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 781 | bash | # How do I change bash history completion to complete what's already on the line?
I found a command a couple of months ago that made my bash history auto-complete on what's already on the line when pressing the up arrow:
```
$ vim fi
```
Press `↑`
```
$ vim file.py
```
I'd like to set this up on my new computer, b... | Probably something like
```
# ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
```
or equivalently,
```
# ~/.bashrc
if [[ $- == *i* ]]
then
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
fi
```
(the if statement checks for interactive mode)
Normally, U... |
2124753 | How can I use PowerShell with the Visual Studio Command Prompt? | 149 | 2010-01-23 21:11:44 | <p>I've been using Beta 2 for a while now and it's been driving me nuts that I have to punt to cmd.exe when running the Visual Studio 2010 Command Prompt. I used to have a nice <em>vsvars2008.ps1</em> script for Visual Studio 2008. Is there a <em>vsvars2010.ps1</em> script or something similar?</p>
| 68,721 | 3,759 | 2024-12-02 18:19:42 | 2,124,759 | 260 | 2010-01-23 21:14:08 | 3,759 | 2022-02-07 20:56:52 | https://stackoverflow.com/q/2124753 | https://stackoverflow.com/a/2124759 | <p>Stealing liberally from blog post <em><a href="http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html" rel="noreferrer">Replace Visual Studio Command Prompt with PowerShell</a></em>, I was able to get this to work. I added the following to my <em>profile.ps1</em> file and all is well with t... | <p>Stealing liberally from blog post <em><a href="http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html" rel="noreferrer">Replace Visual Studio Command Prompt with PowerShell</a></em>, I was able to get this to work. I added the following to my <em>profile.ps1</em> file and all is well with t... | 526, 14456, 33953 | powershell, visual-studio, visual-studio-2010 | <h1>How can I use PowerShell with the Visual Studio Command Prompt?</h1>
<p>I've been using Beta 2 for a while now and it's been driving me nuts that I have to punt to cmd.exe when running the Visual Studio 2010 Command Prompt. I used to have a nice <em>vsvars2008.ps1</em> script for Visual Studio 2008. Is there a <em>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 782 | bash | # How can I use PowerShell with the Visual Studio Command Prompt?
I've been using Beta 2 for a while now and it's been driving me nuts that I have to punt to cmd.exe when running the Visual Studio 2010 Command Prompt. I used to have a nice *vsvars2008.ps1* script for Visual Studio 2008. Is there a *vsvars2010.ps1* scr... | Stealing liberally from blog post *[Replace Visual Studio Command Prompt with PowerShell](http://allen-mack.blogspot.com/2008/03/replace-visual-studio-command-prompt.html)*, I was able to get this to work. I added the following to my *profile.ps1* file and all is well with the world.
```
pushd 'c:\Program Files (x86)\... |
9725675 | Is there a "standard" format for command line/shell help text? | 398 | 2012-03-15 18:14:04 | <p>If not, is there a de facto standard? Basically I'm writing a command line help text like so:</p>
<pre class="lang-none prettyprint-override"><code>usage: app_name [options] required_input required_input2
options:
-a, --argument Does something
-b required Does something with "required"
... | 215,798 | 91,422 | 2024-08-02 07:08:25 | 9,727,046 | 259 | 2012-03-15 19:46:30 | 3,029 | 2019-06-03 08:28:08 | https://stackoverflow.com/q/9725675 | https://stackoverflow.com/a/9727046 | <p>Typically, your help output should include:</p>
<ul>
<li>Description of what the app does</li>
<li>Usage syntax, which:
<ul>
<li>Uses <code>[options]</code> to indicate where the options go</li>
<li><code>arg_name</code> for a required, singular arg</li>
<li><code>[arg_name]</code> for an optional, singular arg</l... | <p>Typically, your help output should include:</p> <ul> <li>Description of what the app does</li> <li>Usage syntax, which: <ul> <li>Uses <code>[options]</code> to indicate where the options go</li> <li><code>arg_name</code> for a required, singular arg</li> <li><code>[arg_name]</code> for an optional, singular arg</l... | 390, 1231, 31134 | command-line, command-line-arguments, shell | <h1>Is there a "standard" format for command line/shell help text?</h1>
<p>If not, is there a de facto standard? Basically I'm writing a command line help text like so:</p>
<pre class="lang-none prettyprint-override"><code>usage: app_name [options] required_input required_input2
options:
-a, --argument Does s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 783 | bash | # Is there a "standard" format for command line/shell help text?
If not, is there a de facto standard? Basically I'm writing a command line help text like so:
```
usage: app_name [options] required_input required_input2
options:
-a, --argument Does something
-b required Does something with "required... | Typically, your help output should include:
- Description of what the app does
- Usage syntax, which:
- Uses `[options]` to indicate where the options go
- `arg_name` for a required, singular arg
- `[arg_name]` for an optional, singular arg
- `arg_name...` for a required arg of which there can be many (this is... |
17988756 | How to select lines between two marker patterns which may occur multiple times with awk/sed | 170 | 2013-08-01 08:25:25 | <p>Using <code>awk</code> or <code>sed</code> how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns.</p>
<p>For example:
Suppose the file contains: </p>
<pre><code>abc
def1
ghi1
jkl1
mno
abc
def2
ghi2
jkl2
mno
pqr
stu
</code></pre>... | 202,407 | 2,099,512 | 2024-08-21 03:34:29 | 17,988,834 | 259 | 2013-08-01 08:29:00 | 1,983,854 | 2017-05-12 10:15:12 | https://stackoverflow.com/q/17988756 | https://stackoverflow.com/a/17988834 | <p>Use <code>awk</code> with a flag to trigger the print when necessary:</p>
<pre><code>$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file
def1
ghi1
jkl1
def2
ghi2
jkl2
</code></pre>
<p>How does this work?</p>
<ul>
<li><code>/abc/</code> matches lines having this text, as well as <code>/mno/</code> does. </li>
<li><c... | <p>Use <code>awk</code> with a flag to trigger the print when necessary:</p> <pre><code>$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file def1 ghi1 jkl1 def2 ghi2 jkl2 </code></pre> <p>How does this work?</p> <ul> <li><code>/abc/</code> matches lines having this text, as well as <code>/mno/</code> does. </li> <li><c... | 34, 390, 990, 5282, 12282 | awk, pattern-matching, sed, shell, unix | <h1>How to select lines between two marker patterns which may occur multiple times with awk/sed</h1>
<p>Using <code>awk</code> or <code>sed</code> how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns.</p>
<p>For example:
Suppose th... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 784 | bash | # How to select lines between two marker patterns which may occur multiple times with awk/sed
Using `awk` or `sed` how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns.
For example:
Suppose the file contains:
```
abc
def1
ghi1
jkl... | Use `awk` with a flag to trigger the print when necessary:
```
$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file
def1
ghi1
jkl1
def2
ghi2
jkl2
```
How does this work?
- `/abc/` matches lines having this text, as well as `/mno/` does.
- `/abc/{flag=1;next}` sets the `flag` when the text `abc` is found. Then, it skips ... |
20536112 | How can I insert a new line in a Linux shell script? | 154 | 2013-12-12 05:59:38 | <p>I want to insert a new line between multiple <a href="https://linux.die.net/man/1/echo" rel="nofollow noreferrer"><code>echo</code></a> statements. I have tried <code>echo "hello\n"</code>, but it is not working. It is printing <code>\n</code>. I want the desired output like this:</p>
<pre class="lang-none... | 726,527 | 3,086,014 | 2025-07-22 15:02:05 | 20,538,015 | 259 | 2013-12-12 08:01:11 | 641,955 | 2016-10-28 11:15:57 | https://stackoverflow.com/q/20536112 | https://stackoverflow.com/a/20538015 | <p>The simplest way to insert a new line between <code>echo</code> statements is to insert an <code>echo</code> without arguments, for example:</p>
<pre><code>echo Create the snapshots
echo
echo Snapshot created
</code></pre>
<p>That is, <code>echo</code> without any arguments will print a blank line.</p>
<p>Another... | <p>The simplest way to insert a new line between <code>echo</code> statements is to insert an <code>echo</code> without arguments, for example:</p> <pre><code>echo Create the snapshots echo echo Snapshot created </code></pre> <p>That is, <code>echo</code> without any arguments will print a blank line.</p> <p>Another... | 58, 387, 3705 | bash, linux, newline | <h1>How can I insert a new line in a Linux shell script?</h1>
<p>I want to insert a new line between multiple <a href="https://linux.die.net/man/1/echo" rel="nofollow noreferrer"><code>echo</code></a> statements. I have tried <code>echo "hello\n"</code>, but it is not working. It is printing <code>\n</code>. ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 785 | bash | # How can I insert a new line in a Linux shell script?
I want to insert a new line between multiple [`echo`](https://linux.die.net/man/1/echo) statements. I have tried `echo "hello\n"`, but it is not working. It is printing `\n`. I want the desired output like this:
```
Create the snapshots
Snapshot created
``` | The simplest way to insert a new line between `echo` statements is to insert an `echo` without arguments, for example:
```
echo Create the snapshots
echo
echo Snapshot created
```
That is, `echo` without any arguments will print a blank line.
Another alternative to use a single `echo` statement with the `-e` flag an... |
8518815 | How to send commands when opening a tmux session inside another tmux session? | 169 | 2011-12-15 10:44:52 | <p>A typical situation may be:</p>
<pre><code>$ tmux
[0] $ ssh example.com
$ tmux attach
[0] $
</code></pre>
<p>I open a tmux session, then ssh in to a server and attach to an existing tmux session. At this point I have one tmux session inside another. How do I send commands to the inner tmux session... | 47,802 | 22,237 | 2017-10-30 09:39:00 | 8,530,024 | 258 | 2011-12-16 05:05:40 | 193,688 | 2011-12-16 05:12:44 | https://stackoverflow.com/q/8518815 | https://stackoverflow.com/a/8530024 | <p>The <code>send-prefix</code> command can be used to send your prefix keystroke to (the process running in) the active pane. By default, the prefix is <strong>C-b</strong> and <strong>C-b</strong> is bound to <code>send-prefix</code> (so that hitting it twice sends a single <strong>C-b</strong> to the active pane). T... | <p>The <code>send-prefix</code> command can be used to send your prefix keystroke to (the process running in) the active pane. By default, the prefix is <strong>C-b</strong> and <strong>C-b</strong> is bound to <code>send-prefix</code> (so that hitting it twice sends a single <strong>C-b</strong> to the active pane). T... | 390, 60238 | shell, tmux | <h1>How to send commands when opening a tmux session inside another tmux session?</h1>
<p>A typical situation may be:</p>
<pre><code>$ tmux
[0] $ ssh example.com
$ tmux attach
[0] $
</code></pre>
<p>I open a tmux session, then ssh in to a server and attach to an existing tmux session. At this point I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 786 | bash | # How to send commands when opening a tmux session inside another tmux session?
A typical situation may be:
```
$ tmux
[0] $ ssh example.com
$ tmux attach
[0] $
```
I open a tmux session, then ssh in to a server and attach to an existing tmux session. At this point I have one tmux session inside anot... | The `send-prefix` command can be used to send your prefix keystroke to (the process running in) the active pane. By default, the prefix is **C-b** and **C-b** is bound to `send-prefix` (so that hitting it twice sends a single **C-b** to the active pane). This is just what we need to access the bindings of the inner *tm... |
54994641 | Openssh Private Key to RSA Private Key | 160 | 2019-03-05 02:40:53 | <p>(I am using MAC)</p>
<p>My id_rsa starts with</p>
<pre><code>-----BEGIN OPENSSH PRIVATE KEY-----
</code></pre>
<p>but I expect it to starts with</p>
<pre><code>-----BEGIN RSA PRIVATE KEY-----
</code></pre>
<p>I have send my id_rsa.pub to server administrator to get the access to server, so I don't want to generate a... | 256,050 | 8,228,358 | 2024-02-14 19:20:42 | 55,817,907 | 258 | 2019-04-23 19:09:26 | 921,747 | 2021-05-17 13:51:13 | https://stackoverflow.com/q/54994641 | https://stackoverflow.com/a/55817907 | <p>You have an OpenSSH format key and want a PEM format key. It is not intuitive to me, but the suggested way to convert is by changing the password for the key and writing it in a different format at the same time.</p>
<p>The command looks like this:</p>
<pre><code>ssh-keygen -p -N "" -m pem -f /path/to/key
... | <p>You have an OpenSSH format key and want a PEM format key. It is not intuitive to me, but the suggested way to convert is by changing the password for the key and writing it in a different format at the same time.</p> <p>The command looks like this:</p> <pre><code>ssh-keygen -p -N "" -m pem -f /path/to/key ... | 369, 386, 387, 3305, 4065 | bash, macos, openssh, rsa, ssh | <h1>Openssh Private Key to RSA Private Key</h1>
<p>(I am using MAC)</p>
<p>My id_rsa starts with</p>
<pre><code>-----BEGIN OPENSSH PRIVATE KEY-----
</code></pre>
<p>but I expect it to starts with</p>
<pre><code>-----BEGIN RSA PRIVATE KEY-----
</code></pre>
<p>I have send my id_rsa.pub to server administrator to get the... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 787 | bash | # Openssh Private Key to RSA Private Key
(I am using MAC)
My id_rsa starts with
```
-----BEGIN OPENSSH PRIVATE KEY-----
```
but I expect it to starts with
```
-----BEGIN RSA PRIVATE KEY-----
```
I have send my id_rsa.pub to server administrator to get the access to server, so I don't want to generate a new key.
... | You have an OpenSSH format key and want a PEM format key. It is not intuitive to me, but the suggested way to convert is by changing the password for the key and writing it in a different format at the same time.
The command looks like this:
```
ssh-keygen -p -N "" -m pem -f /path/to/key
```
It will change the file ... |
20192070 | How to move all files including hidden files into parent directory via * | 152 | 2013-11-25 11:34:55 | <p>Its must be a popular question but I could not find an answer.</p>
<p>How to move all files via * including hidden files as well to parent directory like this:</p>
<pre><code>mv /path/subfolder/* /path/
</code></pre>
<p>This will move all files to parent directory like expected but will not move hidden files. How... | 135,968 | 1,110,341 | 2024-02-20 20:13:09 | 20,192,079 | 258 | 2013-11-25 11:35:44 | 1,983,854 | 2016-10-14 08:34:33 | https://stackoverflow.com/q/20192070 | https://stackoverflow.com/a/20192079 | <p>You can find a comprehensive set of solutions on this in UNIX & Linux's answer to <a href="https://unix.stackexchange.com/a/6397/40596">How do you move all files (including hidden) from one directory to another?</a>. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.</p>
<hr>
<p>You can use thes... | <p>You can find a comprehensive set of solutions on this in UNIX & Linux's answer to <a href="https://unix.stackexchange.com/a/6397/40596">How do you move all files (including hidden) from one directory to another?</a>. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.</p> <hr> <p>You can use thes... | 58, 387, 390, 1796 | bash, command, linux, shell | <h1>How to move all files including hidden files into parent directory via *</h1>
<p>Its must be a popular question but I could not find an answer.</p>
<p>How to move all files via * including hidden files as well to parent directory like this:</p>
<pre><code>mv /path/subfolder/* /path/
</code></pre>
<p>This will mo... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 788 | bash | # How to move all files including hidden files into parent directory via *
Its must be a popular question but I could not find an answer.
How to move all files via * including hidden files as well to parent directory like this:
```
mv /path/subfolder/* /path/
```
This will move all files to parent directory like ex... | You can find a comprehensive set of solutions on this in UNIX & Linux's answer to [How do you move all files (including hidden) from one directory to another?](https://unix.stackexchange.com/a/6397/40596). It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.
---
You can use these two commands together:
... |
6405127 | How do I specify a password to 'psql' non-interactively? | 766 | 2011-06-19 21:06:06 | <p>I am trying to automate database creation process with a shell script and one thing I've hit a road block with passing a password to <a href="https://en.wikipedia.org/wiki/PostgreSQL#Database_administration" rel="noreferrer">psql</a>.
Here is a bit of code from the shell script:</p>
<pre><code>psql -U $DB_USER -h l... | 894,961 | 90,268 | 2026-01-13 14:56:05 | 6,405,162 | 257 | 2011-06-19 21:15:05 | 13,860 | 2017-12-20 17:28:48 | https://stackoverflow.com/q/6405127 | https://stackoverflow.com/a/6405162 | <p>From the <a href="http://www.postgresql.org/docs/8.3/static/app-psql.html" rel="noreferrer">official documentation</a>:</p>
<blockquote>
<p>It is also convenient to have a ~/.pgpass file to avoid regularly having to type in passwords. See <a href="http://www.postgresql.org/docs/8.3/static/libpq-pgpass.html" rel="... | <p>From the <a href="http://www.postgresql.org/docs/8.3/static/app-psql.html" rel="noreferrer">official documentation</a>:</p> <blockquote> <p>It is also convenient to have a ~/.pgpass file to avoid regularly having to type in passwords. See <a href="http://www.postgresql.org/docs/8.3/static/libpq-pgpass.html" rel="... | 256, 387, 1231, 82549 | bash, command-line, postgresql, psql | <h1>How do I specify a password to 'psql' non-interactively?</h1>
<p>I am trying to automate database creation process with a shell script and one thing I've hit a road block with passing a password to <a href="https://en.wikipedia.org/wiki/PostgreSQL#Database_administration" rel="noreferrer">psql</a>.
Here is a bit of... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 789 | bash | # How do I specify a password to 'psql' non-interactively?
I am trying to automate database creation process with a shell script and one thing I've hit a road block with passing a password to [psql](https://en.wikipedia.org/wiki/PostgreSQL#Database_administration).
Here is a bit of code from the shell script:
```
psq... | From the [official documentation](http://www.postgresql.org/docs/8.3/static/app-psql.html):
> It is also convenient to have a ~/.pgpass file to avoid regularly having to type in passwords. See [Section 30.13](http://www.postgresql.org/docs/8.3/static/libpq-pgpass.html) for more information.
...
> This file should co... |
45811971 | warning: ignoring broken ref refs/remotes/origin/HEAD | 175 | 2017-08-22 07:56:22 | <p>Since a few days ago, every time I press <kbd>tab</kbd> key to complete branch names in bash I see the message:</p>
<blockquote>
<p>warning: ignoring broken ref refs/remotes/origin/HEAD warning: ignoring broken ref refs/remotes/origin/HEAD</p>
</blockquote>
<p>For example, this is what I see when I have a branch... | 89,045 | 1,420,625 | 2022-07-12 20:38:09 | 45,867,333 | 257 | 2017-08-24 17:05:09 | 1,696,018 | 2017-08-24 17:05:09 | https://stackoverflow.com/q/45811971 | https://stackoverflow.com/a/45867333 | <p>I encountered this recently when someone on my team deleted our old development branch from the remote. I ran this command to check the status of HEAD:</p>
<pre><code>$ git symbolic-ref refs/remotes/origin/HEAD
refs/remotes/origin/old_dev
</code></pre>
<p>This command output the name of the old development branch,... | <p>I encountered this recently when someone on my team deleted our old development branch from the remote. I ran this command to check the status of HEAD:</p> <pre><code>$ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/old_dev </code></pre> <p>This command output the name of the old development branch,... | 119, 387 | bash, git | <h1>warning: ignoring broken ref refs/remotes/origin/HEAD</h1>
<p>Since a few days ago, every time I press <kbd>tab</kbd> key to complete branch names in bash I see the message:</p>
<blockquote>
<p>warning: ignoring broken ref refs/remotes/origin/HEAD warning: ignoring broken ref refs/remotes/origin/HEAD</p>
</block... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 790 | bash | # warning: ignoring broken ref refs/remotes/origin/HEAD
Since a few days ago, every time I press `tab` key to complete branch names in bash I see the message:
> warning: ignoring broken ref refs/remotes/origin/HEAD warning: ignoring broken ref refs/remotes/origin/HEAD
For example, this is what I see when I have a br... | I encountered this recently when someone on my team deleted our old development branch from the remote. I ran this command to check the status of HEAD:
```
$ git symbolic-ref refs/remotes/origin/HEAD
refs/remotes/origin/old_dev
```
This command output the name of the old development branch, which no longer exists.
I... |
2541767 | What is the proper way to test if a parameter is empty in a batch file? | 312 | 2010-03-29 22:46:36 | <p>I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever <code>%1</code> is surrounded by quotes such as the case when <code>%1</code> is <code>"c:\some path with spaces"</code>.</p>
<pre><code>IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1&quo... | 524,799 | 67,268 | 2024-07-29 14:54:43 | 2,544,545 | 256 | 2010-03-30 10:54:01 | 179,715 | 2017-08-17 20:34:27 | https://stackoverflow.com/q/2541767 | https://stackoverflow.com/a/2544545 | <p>You can use:</p>
<pre><code>IF "%~1" == "" GOTO MyLabel
</code></pre>
<p>to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.</p>
| <p>You can use:</p> <pre><code>IF "%~1" == "" GOTO MyLabel </code></pre> <p>to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.</p> | 64, 390, 2631, 7002 | batch-file, cmd, shell, windows | <h1>What is the proper way to test if a parameter is empty in a batch file?</h1>
<p>I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever <code>%1</code> is surrounded by quotes such as the case when <code>%1</code> is <code>"c:\some path with spaces"</code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 791 | bash | # What is the proper way to test if a parameter is empty in a batch file?
I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever `%1` is surrounded by quotes such as the case when `%1` is `"c:\some path with spaces"`.
```
IF NOT %1 GOTO MyLabel // This is invalid synt... | You can use:
```
IF "%~1" == "" GOTO MyLabel
```
to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it. |
23090390 | Is there anything in Zsh like .bash_profile? | 221 | 2014-04-15 17:20:06 | <p>Everytime I open the terminal, I have to source <code>.bash_profile</code> to enable the <code>$JAVA_HOME</code> or other variables.</p>
| 224,706 | 3,307,896 | 2023-08-12 18:18:21 | 23,091,184 | 256 | 2014-04-15 18:05:29 | 2,553,437 | 2021-08-18 05:38:48 | https://stackoverflow.com/q/23090390 | https://stackoverflow.com/a/23091184 | <p>Yes, it's called <code>~/.zshenv</code>.</p>
<p>Here's how I have <code>$JAVA_HOME</code> set in <code>~/.zshenv</code>:</p>
<pre><code>export JAVA_HOME="$(/usr/libexec/java_home)"
</code></pre>
<p>Keep in mind, however, that zsh is <strong>not</strong> bash, so just 'cause you have to source your <code>.b... | <p>Yes, it's called <code>~/.zshenv</code>.</p> <p>Here's how I have <code>$JAVA_HOME</code> set in <code>~/.zshenv</code>:</p> <pre><code>export JAVA_HOME="$(/usr/libexec/java_home)" </code></pre> <p>Keep in mind, however, that zsh is <strong>not</strong> bash, so just 'cause you have to source your <code>.b... | 387, 3791 | bash, zsh | <h1>Is there anything in Zsh like .bash_profile?</h1>
<p>Everytime I open the terminal, I have to source <code>.bash_profile</code> to enable the <code>$JAVA_HOME</code> or other variables.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 792 | bash | # Is there anything in Zsh like .bash_profile?
Everytime I open the terminal, I have to source `.bash_profile` to enable the `$JAVA_HOME` or other variables. | Yes, it's called `~/.zshenv`.
Here's how I have `$JAVA_HOME` set in `~/.zshenv`:
```
export JAVA_HOME="$(/usr/libexec/java_home)"
```
Keep in mind, however, that zsh is **not** bash, so just 'cause you have to source your `.bash_profile` every time you open a terminal does *not* mean that you have to do that with zs... |
26208231 | Modifying PATH with fish shell | 179 | 2014-10-05 23:19:00 | <p>I'm currently playing around with the <a href="http://fishshell.com/" rel="noreferrer">fish shell</a> and I'm having some trouble wrapping my head around how the <code>PATH</code> variable is set. For what it's worth, I'm also using <a href="https://github.com/oh-my-fish/oh-my-fish/tree/ecf1af6228b3d8ed7dde330412464... | 203,995 | 1,489,726 | 2023-03-27 17:03:09 | 34,401,308 | 256 | 2015-12-21 17:33:17 | 2,188,707 | 2020-06-05 07:15:47 | https://stackoverflow.com/q/26208231 | https://stackoverflow.com/a/34401308 | <p>As stated in the <a href="https://fishshell.com/docs/current/tutorial.html#path" rel="noreferrer">official fish tutorial</a>, you can modify the <code>$fish_user_paths</code> universal variable.</p>
<p>Run the following once from the command-line:</p>
<pre><code>set -U fish_user_paths /usr/local/bin $fish_user_paths... | <p>As stated in the <a href="https://fishshell.com/docs/current/tutorial.html#path" rel="noreferrer">official fish tutorial</a>, you can modify the <code>$fish_user_paths</code> universal variable.</p> <p>Run the following once from the command-line:</p> <pre><code>set -U fish_user_paths /usr/local/bin $fish_user_paths... | 390, 9013, 55824 | environment-variables, fish, shell | <h1>Modifying PATH with fish shell</h1>
<p>I'm currently playing around with the <a href="http://fishshell.com/" rel="noreferrer">fish shell</a> and I'm having some trouble wrapping my head around how the <code>PATH</code> variable is set. For what it's worth, I'm also using <a href="https://github.com/oh-my-fish/oh-my... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 793 | bash | # Modifying PATH with fish shell
I'm currently playing around with the [fish shell](http://fishshell.com/) and I'm having some trouble wrapping my head around how the `PATH` variable is set. For what it's worth, I'm also using [oh-my-fish](https://github.com/oh-my-fish/oh-my-fish/tree/ecf1af6228b3d8ed7dde3304124645b2b... | As stated in the [official fish tutorial](https://fishshell.com/docs/current/tutorial.html#path), you can modify the `$fish_user_paths` universal variable.
Run the following once from the command-line:
```
set -U fish_user_paths /usr/local/bin $fish_user_paths
```
This will prepend `/usr/local/bin` permanently to yo... |
4090301 | Root user/sudo equivalent in Cygwin? | 174 | 2010-11-03 18:24:42 | <p>I'm trying to run a bash script in Cygwin. </p>
<p>I get <code>Must run as root, i.e. sudo ./scriptname</code> errors. </p>
<p><code>chmod 777 scriptname</code> does nothing to help. </p>
<p>I've looked for ways to imitate sudo on Cygwin, to add a root user, since calling "su" renders the error <code>su: user ... | 197,628 | 470,925 | 2022-11-05 18:32:41 | 21,024,592 | 256 | 2014-01-09 15:40:11 | 343,302 | 2020-01-31 11:28:26 | https://stackoverflow.com/q/4090301 | https://stackoverflow.com/a/21024592 | <p>I answered this question on <a href="https://superuser.com/a/699281/93684">SuperUser</a> but only after the OP disregarded the unhelpful answer that was at the time the only answer to the question.</p>
<p><strong>Here is the proper way to elevate permissions in Cygwin, copied from my own answer on SuperUser:</stron... | <p>I answered this question on <a href="https://superuser.com/a/699281/93684">SuperUser</a> but only after the OP disregarded the unhelpful answer that was at the time the only answer to the question.</p> <p><strong>Here is the proper way to elevate permissions in Cygwin, copied from my own answer on SuperUser:</stron... | 387, 1674, 1731, 12079 | bash, cygwin, root, sudo | <h1>Root user/sudo equivalent in Cygwin?</h1>
<p>I'm trying to run a bash script in Cygwin. </p>
<p>I get <code>Must run as root, i.e. sudo ./scriptname</code> errors. </p>
<p><code>chmod 777 scriptname</code> does nothing to help. </p>
<p>I've looked for ways to imitate sudo on Cygwin, to add a root user, since ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 794 | bash | # Root user/sudo equivalent in Cygwin?
I'm trying to run a bash script in Cygwin.
I get `Must run as root, i.e. sudo ./scriptname` errors.
`chmod 777 scriptname` does nothing to help.
I've looked for ways to imitate sudo on Cygwin, to add a root user, since calling "su" renders the error `su: user root does not exi... | I answered this question on [SuperUser](https://superuser.com/a/699281/93684) but only after the OP disregarded the unhelpful answer that was at the time the only answer to the question.
**Here is the proper way to elevate permissions in Cygwin, copied from my own answer on SuperUser:**
I found the answer on [the Cyg... |
3249827 | Convert Unix timestamp to a date string | 173 | 2010-07-14 19:41:06 | <p>Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line?</p>
<p><code>date</code> might work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work properly. It seems like there might be an easier way —... | 219,852 | 391,126 | 2024-12-01 09:30:44 | 3,249,936 | 256 | 2010-07-14 19:54:34 | 68,587 | 2024-08-13 23:29:49 | https://stackoverflow.com/q/3249827 | https://stackoverflow.com/a/3249936 | <p>With <code>date</code> from <a href="https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html" rel="noreferrer">GNU coreutils</a> you can do:</p>
<pre><code>date -d "@$TIMESTAMP"
</code></pre>
<pre><code># date -d @0
Wed Dec 31 19:00:00 EST 1969
</code></pre>
<p>(From: <a href=... | <p>With <code>date</code> from <a href="https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html" rel="noreferrer">GNU coreutils</a> you can do:</p> <pre><code>date -d "@$TIMESTAMP" </code></pre> <pre><code># date -d @0 Wed Dec 31 19:00:00 EST 1969 </code></pre> <p>(From: <a href=... | 34, 387, 390, 32842 | bash, shell, unix, unix-timestamp | <h1>Convert Unix timestamp to a date string</h1>
<p>Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line?</p>
<p><code>date</code> might work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work prope... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 795 | bash | # Convert Unix timestamp to a date string
Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line?
`date` might work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work properly. It seems like there m... | With `date` from [GNU coreutils](https://www.gnu.org/software/coreutils/manual/html_node/Seconds-since-the-Epoch.html) you can do:
```
date -d "@$TIMESTAMP"
```
```
# date -d @0
Wed Dec 31 19:00:00 EST 1969
```
(From: [BASH: Convert Unix Timestamp to a Date](https://web.archive.org/web/20200930143943/https://www.ant... |
13195655 | Bash set +x without it being printed | 169 | 2012-11-02 12:54:12 | <p>Does anyone know if we can say <code>set +x</code> in bash without it being printed:</p>
<pre><code>set -x
command
set +x
</code></pre>
<p>traces</p>
<pre><code>+ command
+ set +x
</code></pre>
<p>but it should just print</p>
<pre><code>+ command
</code></pre>
<p>Bash is Version 4.1.10(4). This is bugging me f... | 47,477 | 887,771 | 2022-12-08 20:44:34 | 19,226,038 | 256 | 2013-10-07 13:21:30 | 2,854,850 | 2013-10-07 13:39:05 | https://stackoverflow.com/q/13195655 | https://stackoverflow.com/a/19226038 | <p>I had the same problem, and I was able to find a solution that doesn't use a subshell:</p>
<pre><code>set -x
command
{ set +x; } 2>/dev/null
</code></pre>
| <p>I had the same problem, and I was able to find a solution that doesn't use a subshell:</p> <pre><code>set -x command { set +x; } 2>/dev/null </code></pre> | 387, 390 | bash, shell | <h1>Bash set +x without it being printed</h1>
<p>Does anyone know if we can say <code>set +x</code> in bash without it being printed:</p>
<pre><code>set -x
command
set +x
</code></pre>
<p>traces</p>
<pre><code>+ command
+ set +x
</code></pre>
<p>but it should just print</p>
<pre><code>+ command
</code></pre>
<p>B... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 796 | bash | # Bash set +x without it being printed
Does anyone know if we can say `set +x` in bash without it being printed:
```
set -x
command
set +x
```
traces
```
+ command
+ set +x
```
but it should just print
```
+ command
```
Bash is Version 4.1.10(4). This is bugging me for some time now - output is cluttered with us... | I had the same problem, and I was able to find a solution that doesn't use a subshell:
```
set -x
command
{ set +x; } 2>/dev/null
``` |
10024279 | How to use shell commands in Makefile | 166 | 2012-04-05 07:25:53 | <p>I'm trying to use the result of <code>ls</code> in other commands (e.g. echo, rsync):</p>
<pre><code>all:
<Building, creating some .tgz files - removed for clarity>
FILES = $(shell ls)
echo $(FILES)
</code></pre>
<p>But I get:</p>
<pre><code>make
FILES = Makefile file1.tgz file2.tgz file3.tgz
ma... | 374,735 | 51,197 | 2023-10-08 12:11:13 | 10,024,479 | 256 | 2012-04-05 07:39:00 | 1,256,452 | 2017-07-23 20:30:22 | https://stackoverflow.com/q/10024279 | https://stackoverflow.com/a/10024479 | <p>With:</p>
<pre><code>FILES = $(shell ls)
</code></pre>
<p>indented underneath <code>all</code> like that, it's a build command. So this expands <code>$(shell ls)</code>, then tries to run the command <code>FILES ...</code>.</p>
<p>If <code>FILES</code> is supposed to be a <code>make</code> variable, these variab... | <p>With:</p> <pre><code>FILES = $(shell ls) </code></pre> <p>indented underneath <code>all</code> like that, it's a build command. So this expands <code>$(shell ls)</code>, then tries to run the command <code>FILES ...</code>.</p> <p>If <code>FILES</code> is supposed to be a <code>make</code> variable, these variab... | 387, 4301, 13824 | bash, echo, makefile | <h1>How to use shell commands in Makefile</h1>
<p>I'm trying to use the result of <code>ls</code> in other commands (e.g. echo, rsync):</p>
<pre><code>all:
<Building, creating some .tgz files - removed for clarity>
FILES = $(shell ls)
echo $(FILES)
</code></pre>
<p>But I get:</p>
<pre><code>make
FI... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 797 | bash | # How to use shell commands in Makefile
I'm trying to use the result of `ls` in other commands (e.g. echo, rsync):
```
all:
<Building, creating some .tgz files - removed for clarity>
FILES = $(shell ls)
echo $(FILES)
```
But I get:
```
make
FILES = Makefile file1.tgz file2.tgz file3.tgz
make: FILES: No ... | With:
```
FILES = $(shell ls)
```
indented underneath `all` like that, it's a build command. So this expands `$(shell ls)`, then tries to run the command `FILES ...`.
If `FILES` is supposed to be a `make` variable, these variables need to be assigned outside the recipe portion, e.g.:
```
FILES = $(shell ls)
all:
... |
6509650 | Extract directory from path | 162 | 2011-06-28 16:14:54 | <p>In my script I need the <strong>directory</strong> of the file I am working with. For example, the <em>file</em>=<strong>"stuff/backup/file.zip"</strong>. I need a way to get the string "<strong>stuff/backup/</strong>" from the variable <code>$file</code>. </p>
| 112,644 | 689,317 | 2024-03-19 13:25:04 | 6,509,677 | 256 | 2011-06-28 16:16:43 | 347,565 | 2024-03-19 13:25:04 | https://stackoverflow.com/q/6509650 | https://stackoverflow.com/a/6509677 | <pre class="lang-bash prettyprint-override"><code>dirname -- "$file";
</code></pre>
<p>is what you are looking for</p>
| <pre class="lang-bash prettyprint-override"><code>dirname -- "$file"; </code></pre> <p>is what you are looking for</p> | 139, 387, 6268 | bash, path, string | <h1>Extract directory from path</h1>
<p>In my script I need the <strong>directory</strong> of the file I am working with. For example, the <em>file</em>=<strong>"stuff/backup/file.zip"</strong>. I need a way to get the string "<strong>stuff/backup/</strong>" from the variable <code>$file</code>. </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 798 | bash | # Extract directory from path
In my script I need the **directory** of the file I am working with. For example, the *file*=**"stuff/backup/file.zip"**. I need a way to get the string "**stuff/backup/**" from the variable `$file`. | ```
dirname -- "$file";
```
is what you are looking for |
3274397 | Reload .profile in bash shell script (in unix)? | 154 | 2010-07-18 05:01:53 | <p>I'm new to bash shell scripting, and have come across a challenge. I know I can reload my ".profile" file by just doing:</p>
<pre><code>. .profile
</code></pre>
<p>but I'm trying to execute the same in a bash script I'm writing and it is just not working.
Any ideas? Anything else I can provide to clarify?</p>
<p... | 239,269 | 394,988 | 2017-10-23 15:38:55 | 9,354,275 | 256 | 2012-02-19 23:12:54 | 1,219,894 | 2012-02-20 12:41:55 | https://stackoverflow.com/q/3274397 | https://stackoverflow.com/a/9354275 | <p>Try this to reload your current shell:</p>
<pre><code>source ~/.profile
</code></pre>
| <p>Try this to reload your current shell:</p> <pre><code>source ~/.profile </code></pre> | 34, 387, 390 | bash, shell, unix | <h1>Reload .profile in bash shell script (in unix)?</h1>
<p>I'm new to bash shell scripting, and have come across a challenge. I know I can reload my ".profile" file by just doing:</p>
<pre><code>. .profile
</code></pre>
<p>but I'm trying to execute the same in a bash script I'm writing and it is just not working.
A... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 799 | bash | # Reload .profile in bash shell script (in unix)?
I'm new to bash shell scripting, and have come across a challenge. I know I can reload my ".profile" file by just doing:
```
. .profile
```
but I'm trying to execute the same in a bash script I'm writing and it is just not working.
Any ideas? Anything else I can prov... | Try this to reload your current shell:
```
source ~/.profile
``` |
3314660 | Passing variables in remote ssh command | 137 | 2010-07-23 01:03:05 | <p>I want to be able to run a command from my machine using ssh and pass through the environment variable <code>$BUILD_NUMBER</code></p>
<p>Here's what I'm trying:</p>
<pre><code>ssh pvt@192.168.1.133 '~/tools/myScript.pl $BUILD_NUMBER'
</code></pre>
<p><code>$BUILD_NUMBER</code> is set on the machine making the ssh... | 193,653 | 102,641 | 2022-12-29 16:20:39 | 3,314,678 | 256 | 2010-07-23 01:06:38 | 377,270 | 2010-07-23 01:06:38 | https://stackoverflow.com/q/3314660 | https://stackoverflow.com/a/3314678 | <p>If you use</p>
<pre><code>ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER"
</code></pre>
<p>instead of</p>
<pre><code>ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER'
</code></pre>
<p>your shell will interpolate the <code>$BUILD_NUMBER</code> before sending the command string to the remote host.<... | <p>If you use</p> <pre><code>ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER" </code></pre> <p>instead of</p> <pre><code>ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER' </code></pre> <p>your shell will interpolate the <code>$BUILD_NUMBER</code> before sending the command string to the remote host.<... | 58, 386, 387, 390 | bash, linux, shell, ssh | <h1>Passing variables in remote ssh command</h1>
<p>I want to be able to run a command from my machine using ssh and pass through the environment variable <code>$BUILD_NUMBER</code></p>
<p>Here's what I'm trying:</p>
<pre><code>ssh pvt@192.168.1.133 '~/tools/myScript.pl $BUILD_NUMBER'
</code></pre>
<p><code>$BUILD_N... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 800 | bash | # Passing variables in remote ssh command
I want to be able to run a command from my machine using ssh and pass through the environment variable `$BUILD_NUMBER`
Here's what I'm trying:
```
ssh pvt@192.168.1.133 '~/tools/myScript.pl $BUILD_NUMBER'
```
`$BUILD_NUMBER` is set on the machine making the ssh call and sin... | If you use
```
ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER"
```
instead of
```
ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER'
```
your shell will interpolate the `$BUILD_NUMBER` before sending the command string to the remote host. |
18421757 | live output from subprocess command | 319 | 2013-08-24 18:27:18 | <p>I'm using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use <code>subprocess.Popen</code> to run the code, collect the output from <code>stdout</code> and <code>stderr</code> into a <code>subprocess.PIPE</code> --- then I can print (and save to a log-file) the out... | 312,642 | 230,468 | 2025-11-18 15:48:08 | 18,422,264 | 255 | 2013-08-24 19:23:08 | 2,199,958 | 2022-05-14 14:21:56 | https://stackoverflow.com/q/18421757 | https://stackoverflow.com/a/18422264 | <p>TLDR for Python 3:</p>
<pre class="lang-py prettyprint-override"><code>import subprocess
import sys
with open("test.log", "wb") as f:
process = subprocess.Popen(your_command, stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), b""):
sys.stdout.buffer.w... | <p>TLDR for Python 3:</p> <pre class="lang-py prettyprint-override"><code>import subprocess import sys with open("test.log", "wb") as f: process = subprocess.Popen(your_command, stdout=subprocess.PIPE) for c in iter(lambda: process.stdout.read(1), b""): sys.stdout.buffer.w... | 16, 379, 390, 942, 2348 | error-handling, logging, python, shell, subprocess | <h1>live output from subprocess command</h1>
<p>I'm using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use <code>subprocess.Popen</code> to run the code, collect the output from <code>stdout</code> and <code>stderr</code> into a <code>subprocess.PIPE</code> --- then... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 801 | bash | # live output from subprocess command
I'm using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use `subprocess.Popen` to run the code, collect the output from `stdout` and `stderr` into a `subprocess.PIPE` --- then I can print (and save to a log-file) the output infor... | TLDR for Python 3:
```
import subprocess
import sys
with open("test.log", "wb") as f:
process = subprocess.Popen(your_command, stdout=subprocess.PIPE)
for c in iter(lambda: process.stdout.read(1), b""):
sys.stdout.buffer.write(c)
f.buffer.write(c)
```
---
You have two ways of doing this, eit... |
2043453 | Executing multi-line statements in the one-line command-line | 278 | 2010-01-11 17:11:27 | <p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p>
<pre class="lang-bash prettyprint-override"><code>python -c "for r in range(10): print 'rob'"
</code></pre>
<p>This works fine. However, if I import a module before the <em>for</em> loop, I get a syntax error:</p>
<pre class="lang... | 236,106 | null | 2025-08-26 01:15:34 | 2,043,499 | 255 | 2010-01-11 17:18:43 | 235,354 | 2025-08-26 01:05:41 | https://stackoverflow.com/q/2043453 | https://stackoverflow.com/a/2043499 | <p>You could do</p>
<pre class="lang-bash prettyprint-override"><code>echo -e "import sys\nfor r in range(10): print 'rob'" | python
</code></pre>
<p>Or without pipes:</p>
<pre class="lang-bash prettyprint-override"><code>python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")"
</... | <p>You could do</p> <pre class="lang-bash prettyprint-override"><code>echo -e "import sys\nfor r in range(10): print 'rob'" | python </code></pre> <p>Or without pipes:</p> <pre class="lang-bash prettyprint-override"><code>python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")" </... | 16, 390, 1231 | command-line, python, shell | <h1>Executing multi-line statements in the one-line command-line</h1>
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p>
<pre class="lang-bash prettyprint-override"><code>python -c "for r in range(10): print 'rob'"
</code></pre>
<p>This works fine. However, if I import a module be... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 802 | bash | # Executing multi-line statements in the one-line command-line
I'm using Python with `-c` to execute a one-liner loop, i.e.:
```
python -c "for r in range(10): print 'rob'"
```
This works fine. However, if I import a module before the *for* loop, I get a syntax error:
```
python -c "import sys; for r in range(10): ... | You could do
```
echo -e "import sys\nfor r in range(10): print 'rob'" | python
```
Or without pipes:
```
python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")"
```
Or
```
(echo "import sys" ; echo "for r in range(10): print 'rob'") | python
```
Or [SilentGhost's answer](https://stackoverflow.com/quest... |
1624691 | Linux: kill background task | 228 | 2009-10-26 13:10:45 | <p>How do I kill the last spawned background task in Linux?</p>
<p>Example:</p>
<pre><code>doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
</code></pre>
| 192,254 | 63,051 | 2023-03-11 11:20:48 | 1,624,713 | 255 | 2009-10-26 13:14:39 | 46,991 | 2023-03-11 10:39:04 | https://stackoverflow.com/q/1624691 | https://stackoverflow.com/a/1624713 | <p>There's a special variable for this in Bash:</p>
<pre><code>kill $!
</code></pre>
<p>$! expands to the PID of the last process executed in the background.</p>
| <p>There's a special variable for this in Bash:</p> <pre><code>kill $! </code></pre> <p>$! expands to the PID of the last process executed in the background.</p> | 34, 58, 387, 5779, 27223 | bash, job-control, kill, linux, unix | <h1>Linux: kill background task</h1>
<p>How do I kill the last spawned background task in Linux?</p>
<p>Example:</p>
<pre><code>doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 803 | bash | # Linux: kill background task
How do I kill the last spawned background task in Linux?
Example:
```
doSomething
doAnotherThing
doB &
doC
doD
#kill doB
????
``` | There's a special variable for this in Bash:
```
kill $!
```
$! expands to the PID of the last process executed in the background. |
8385627 | Subtract two variables in Bash | 282 | 2011-12-05 12:55:01 | <p>I have the script below to subtract the counts of files between two directories but the <code>COUNT=</code> expression does not work. What is the correct syntax?</p>
<pre><code>#!/usr/bin/env bash
FIRSTV=`ls -1 | wc -l`
cd ..
SECONDV=`ls -1 | wc -l`
COUNT=expr $FIRSTV-$SECONDV ## -> gives 'command not found' e... | 446,544 | 688,412 | 2024-07-11 22:04:21 | 8,386,731 | 254 | 2011-12-05 14:24:11 | 146,041 | 2018-05-28 12:48:05 | https://stackoverflow.com/q/8385627 | https://stackoverflow.com/a/8386731 | <p>You just need a little extra whitespace around the minus sign, and backticks:</p>
<pre><code>COUNT=`expr $FIRSTV - $SECONDV`
</code></pre>
<p>Be aware of the exit status:</p>
<p>The exit status is 0 if EXPRESSION is neither null nor 0, <strong>1 if EXPRESSION is null or 0</strong>.</p>
<p>Keep this in mind when ... | <p>You just need a little extra whitespace around the minus sign, and backticks:</p> <pre><code>COUNT=`expr $FIRSTV - $SECONDV` </code></pre> <p>Be aware of the exit status:</p> <p>The exit status is 0 if EXPRESSION is neither null nor 0, <strong>1 if EXPRESSION is null or 0</strong>.</p> <p>Keep this in mind when ... | 34, 387, 390 | bash, shell, unix | <h1>Subtract two variables in Bash</h1>
<p>I have the script below to subtract the counts of files between two directories but the <code>COUNT=</code> expression does not work. What is the correct syntax?</p>
<pre><code>#!/usr/bin/env bash
FIRSTV=`ls -1 | wc -l`
cd ..
SECONDV=`ls -1 | wc -l`
COUNT=expr $FIRSTV-$SECON... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 805 | bash | # Subtract two variables in Bash
I have the script below to subtract the counts of files between two directories but the `COUNT=` expression does not work. What is the correct syntax?
```
#!/usr/bin/env bash
FIRSTV=`ls -1 | wc -l`
cd ..
SECONDV=`ls -1 | wc -l`
COUNT=expr $FIRSTV-$SECONDV ## -> gives 'command not fo... | You just need a little extra whitespace around the minus sign, and backticks:
```
COUNT=`expr $FIRSTV - $SECONDV`
```
Be aware of the exit status:
The exit status is 0 if EXPRESSION is neither null nor 0, **1 if EXPRESSION is null or 0**.
Keep this in mind when using the expression in a bash script in combination w... |
9449778 | What is the benefit of using $() instead of backticks in shell scripts? | 256 | 2012-02-26 01:37:32 | <p>There are two ways to capture the output of command line in <code>bash</code>:</p>
<ol>
<li><p>Legacy Bourne shell backticks <code>``</code>:</p>
<pre><code>var=`command`
</code></pre></li>
<li><p><code>$()</code> syntax (which as far as I know is Bash specific, or at least not supported by non-POSIX old shells li... | 115,979 | 119,280 | 2021-11-25 22:11:25 | 9,449,788 | 254 | 2012-02-26 01:40:13 | 14,860 | 2012-02-26 02:35:28 | https://stackoverflow.com/q/9449778 | https://stackoverflow.com/a/9449788 | <p>The major one is the ability to <em>nest</em> them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks.</p>
<p>An example, though somewhat contrived:</p>
<pre><code>deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)
... | <p>The major one is the ability to <em>nest</em> them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks.</p> <p>An example, though somewhat contrived:</p> <pre><code>deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print) ... | 367, 387, 10327, 28438 | backticks, bash, sh, syntax | <h1>What is the benefit of using $() instead of backticks in shell scripts?</h1>
<p>There are two ways to capture the output of command line in <code>bash</code>:</p>
<ol>
<li><p>Legacy Bourne shell backticks <code>``</code>:</p>
<pre><code>var=`command`
</code></pre></li>
<li><p><code>$()</code> syntax (which as far... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 806 | bash | # What is the benefit of using $() instead of backticks in shell scripts?
There are two ways to capture the output of command line in `bash`:
1. Legacy Bourne shell backticks ``` `` ```:
```
var=`command`
```
2. `$()` syntax (which as far as I know is Bash specific, or at least not supported by non-POSIX ol... | The major one is the ability to *nest* them, commands within commands, without losing your sanity trying to figure out if some form of escaping will work on the backticks.
An example, though somewhat contrived:
```
deps=$(find /dir -name $(ls -1tr 201112[0-9][0-9]*.txt | tail -1l) -print)
```
which will give you a l... |
21129746 | Multiline comment in PowerShell | 150 | 2014-01-15 05:29:04 | <p>Can we comment multiple lines together in PowerShell?</p>
<p>I tried looking, but I didn't find any answer. It's quite irritating to comment each line manually if the script is too long.</p>
| 85,496 | 1,872,084 | 2022-07-08 19:49:40 | 21,129,768 | 254 | 2014-01-15 05:31:19 | 2,684,760 | 2020-04-01 05:04:30 | https://stackoverflow.com/q/21129746 | https://stackoverflow.com/a/21129768 | <p>In PowerShell v2 and newer, use the following syntax for the multiline comments:</p>
<pre><code><# a
b
c #>
</code></pre>
| <p>In PowerShell v2 and newer, use the following syntax for the multiline comments:</p> <pre><code><# a b c #> </code></pre> | 526, 24067 | powershell, powershell-2.0 | <h1>Multiline comment in PowerShell</h1>
<p>Can we comment multiple lines together in PowerShell?</p>
<p>I tried looking, but I didn't find any answer. It's quite irritating to comment each line manually if the script is too long.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 807 | bash | # Multiline comment in PowerShell
Can we comment multiple lines together in PowerShell?
I tried looking, but I didn't find any answer. It's quite irritating to comment each line manually if the script is too long. | In PowerShell v2 and newer, use the following syntax for the multiline comments:
```
<# a
b
c #>
``` |
3871332 | How to tell bash that the line continues on the next line | 201 | 2010-10-06 09:57:08 | <p>In a bash script I got from another programmer, some lines exceeded 80 columns in length. What is the character or thing to be added to the line in order to indicate that the line continues on the next line?</p>
| 270,958 | 171,546 | 2017-09-12 10:25:02 | 3,871,336 | 253 | 2010-10-06 09:58:04 | 23,704 | 2013-10-19 23:03:24 | https://stackoverflow.com/q/3871332 | https://stackoverflow.com/a/3871336 | <p>The character is a backslash <code>\</code></p>
<p>From the <a href="http://www.gnu.org/software/bash/manual/bashref.html" rel="noreferrer">bash manual</a>:</p>
<blockquote>
<p>The backslash character ‘\’ may be used to remove any special meaning
for the next character read and for line continuation.</p>
</blo... | <p>The character is a backslash <code>\</code></p> <p>From the <a href="http://www.gnu.org/software/bash/manual/bashref.html" rel="noreferrer">bash manual</a>:</p> <blockquote> <p>The backslash character ‘\’ may be used to remove any special meaning for the next character read and for line continuation.</p> </blo... | 387 | bash | <h1>How to tell bash that the line continues on the next line</h1>
<p>In a bash script I got from another programmer, some lines exceeded 80 columns in length. What is the character or thing to be added to the line in order to indicate that the line continues on 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 | 808 | bash | # How to tell bash that the line continues on the next line
In a bash script I got from another programmer, some lines exceeded 80 columns in length. What is the character or thing to be added to the line in order to indicate that the line continues on the next line? | The character is a backslash `\`
From the [bash manual](http://www.gnu.org/software/bash/manual/bashref.html):
> The backslash character ‘\’ may be used to remove any special meaning
> for the next character read and for line continuation. |
4724290 | PowerShell: Run command from script's directory | 189 | 2011-01-18 12:55:41 | <p>I have a PowerShell script that does some stuff using the script’s current directory. So when inside that directory, running <code>.\script.ps1</code> works correctly.</p>
<p>Now I want to call that script from a different directory without changing the referencing directory of the script. So I want to call <code>.... | 338,468 | 216,074 | 2023-11-10 13:36:55 | 4,724,421 | 253 | 2011-01-18 13:10:55 | 193,978 | 2021-04-22 17:54:07 | https://stackoverflow.com/q/4724290 | https://stackoverflow.com/a/4724421 | <p>Do you mean you want the script's own path so you can reference a file next to the script? Try this:</p>
<pre><code>$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Write-host "My directory is $dir"
</code></pre>
<p>You can get a lot of info from $MyInvocation and its properties.</... | <p>Do you mean you want the script's own path so you can reference a file next to the script? Try this:</p> <pre><code>$scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath Write-host "My directory is $dir" </code></pre> <p>You can get a lot of info from $MyInvocation and its properties.</... | 526 | powershell | <h1>PowerShell: Run command from script's directory</h1>
<p>I have a PowerShell script that does some stuff using the script’s current directory. So when inside that directory, running <code>.\script.ps1</code> works correctly.</p>
<p>Now I want to call that script from a different directory without changing the refer... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 809 | bash | # PowerShell: Run command from script's directory
I have a PowerShell script that does some stuff using the script’s current directory. So when inside that directory, running `.\script.ps1` works correctly.
Now I want to call that script from a different directory without changing the referencing directory of the scr... | Do you mean you want the script's own path so you can reference a file next to the script? Try this:
```
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Write-host "My directory is $dir"
```
You can get a lot of info from $MyInvocation and its properties.
If you want to reference a file in t... |
38021348 | How can I 'echo' out things without a newline? | 145 | 2016-06-24 20:09:48 | <p>I have the following code:</p>
<pre><code>for x in "${array[@]}"
do
echo "$x"
done
</code></pre>
<p>The results are something like this (I sort these later in some cases):</p>
<pre><code>1
2
3
4
5
</code></pre>
<p>Is there a way to print it as <code>1 2 3 4 5</code> instead? Without adding a ne... | 237,139 | 6,263,005 | 2025-03-21 14:08:38 | 38,021,361 | 253 | 2016-06-24 20:10:56 | 3,789,550 | 2021-02-26 06:15:21 | https://stackoverflow.com/q/38021348 | https://stackoverflow.com/a/38021361 | <p>Yes. Use the <code>-n</code> option:</p>
<pre><code>echo -n "$x"
</code></pre>
<p>From <code>help echo</code>:</p>
<blockquote>
<p>-n do not append a newline</p>
</blockquote>
<p>This would strips off the last newline too, so if you want you can add a final newline after the loop:</p>
<pre><code>for ...; d... | <p>Yes. Use the <code>-n</code> option:</p> <pre><code>echo -n "$x" </code></pre> <p>From <code>help echo</code>:</p> <blockquote> <p>-n do not append a newline</p> </blockquote> <p>This would strips off the last newline too, so if you want you can add a final newline after the loop:</p> <pre><code>for ...; d... | 387 | bash | <h1>How can I 'echo' out things without a newline?</h1>
<p>I have the following code:</p>
<pre><code>for x in "${array[@]}"
do
echo "$x"
done
</code></pre>
<p>The results are something like this (I sort these later in some cases):</p>
<pre><code>1
2
3
4
5
</code></pre>
<p>Is there a way to print i... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 810 | bash | # How can I 'echo' out things without a newline?
I have the following code:
```
for x in "${array[@]}"
do
echo "$x"
done
```
The results are something like this (I sort these later in some cases):
```
1
2
3
4
5
```
Is there a way to print it as `1 2 3 4 5` instead? Without adding a newline every time? | Yes. Use the `-n` option:
```
echo -n "$x"
```
From `help echo`:
> -n do not append a newline
This would strips off the last newline too, so if you want you can add a final newline after the loop:
```
for ...; do ...; done; echo
```
---
**Note:**
This is not portable among various implementations of `echo` buil... |
21985492 | Recursively change file extensions in Bash | 131 | 2014-02-24 10:45:35 | <p>I want to recursively iterate through a directory and change the extension of all files of a certain extension, say <code>.t1</code> to <code>.t2</code>. What is the bash command for doing this?</p>
| 87,110 | 761,350 | 2024-03-11 09:25:30 | 21,985,531 | 253 | 2014-02-24 10:47:07 | 548,225 | 2024-03-11 09:25:30 | https://stackoverflow.com/q/21985492 | https://stackoverflow.com/a/21985531 | <p>Use:</p>
<pre class="lang-bash prettyprint-override"><code>find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +
</code></pre>
<p>If you have <code>rename</code> available then use <strong>one</strong> of these:</p>
<pre class="lang-bash prettyprint-override"><code>find . -... | <p>Use:</p> <pre class="lang-bash prettyprint-override"><code>find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' + </code></pre> <p>If you have <code>rename</code> available then use <strong>one</strong> of these:</p> <pre class="lang-bash prettyprint-override"><code>find . -... | 18, 58, 387, 390, 10327 | bash, linux, regex, sh, shell | <h1>Recursively change file extensions in Bash</h1>
<p>I want to recursively iterate through a directory and change the extension of all files of a certain extension, say <code>.t1</code> to <code>.t2</code>. What is the bash command for doing this?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 811 | bash | # Recursively change file extensions in Bash
I want to recursively iterate through a directory and change the extension of all files of a certain extension, say `.t1` to `.t2`. What is the bash command for doing this? | Use:
```
find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +
```
If you have `rename` available then use **one** of these:
```
find . -name '*.t1' -exec rename .t1 .t2 {} +
```
```
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' +
```
---
For a single file use the `+` delimiter and for renamin... |
1815329 | Portable way to get file size (in bytes) in the shell | 152 | 2009-11-29 11:47:18 | <p>On Linux, I use <code>stat --format="%s" FILE</code>, but the <a href="https://en.wikipedia.org/wiki/Solaris_%28operating_system%29" rel="noreferrer">Solaris</a> machine I have access to doesn't have the <code>stat</code> command. What should I use then?</p>
<p>I'm writing Bash scripts and can't really ins... | 230,875 | null | 2025-03-10 16:21:03 | 1,815,582 | 252 | 2009-11-29 13:45:21 | 172,211 | 2017-07-04 08:41:22 | https://stackoverflow.com/q/1815329 | https://stackoverflow.com/a/1815582 | <p><code>wc -c < filename</code> (short for word count, <code>-c</code> prints the byte count) is a portable, <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/wc.html" rel="noreferrer">POSIX</a> solution. Only the output format might not be uniform across platforms as some spaces may be prepended (... | <p><code>wc -c < filename</code> (short for word count, <code>-c</code> prints the byte count) is a portable, <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/wc.html" rel="noreferrer">POSIX</a> solution. Only the output format might not be uniform across platforms as some spaces may be prepended (... | 58, 387, 390, 1407, 2635 | bash, linux, portability, shell, solaris | <h1>Portable way to get file size (in bytes) in the shell</h1>
<p>On Linux, I use <code>stat --format="%s" FILE</code>, but the <a href="https://en.wikipedia.org/wiki/Solaris_%28operating_system%29" rel="noreferrer">Solaris</a> machine I have access to doesn't have the <code>stat</code> command. What should I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 812 | bash | # Portable way to get file size (in bytes) in the shell
On Linux, I use `stat --format="%s" FILE`, but the [Solaris](https://en.wikipedia.org/wiki/Solaris_%28operating_system%29) machine I have access to doesn't have the `stat` command. What should I use then?
I'm writing Bash scripts and can't really install any new... | `wc -c < filename` (short for word count, `-c` prints the byte count) is a portable, [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/wc.html) solution. Only the output format might not be uniform across platforms as some spaces may be prepended (which is the case for Solaris).
Do not omit the input r... |
13181725 | Append file contents to the bottom of existing file in Bash | 124 | 2012-11-01 16:46:34 | <blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/5586293/shell-script-to-append-text-to-each-file">Shell script to append text to each file?</a><br>
<a href="https://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-text-file-in-shell-scrip... | 198,442 | 718,534 | 2019-03-18 08:16:48 | 13,181,820 | 252 | 2012-11-01 16:52:26 | 140,750 | 2012-11-01 16:52:26 | https://stackoverflow.com/q/13181725 | https://stackoverflow.com/a/13181820 | <p>This should work:</p>
<pre><code> cat "$API" >> "$CONFIG"
</code></pre>
<p>You need to use the <code>>></code> operator to append to a file. Redirecting with <code>></code> causes the file to be overwritten. (truncated).</p>
| <p>This should work:</p> <pre><code> cat "$API" >> "$CONFIG" </code></pre> <p>You need to use the <code>>></code> operator to append to a file. Redirecting with <code>></code> causes the file to be overwritten. (truncated).</p> | 387, 990, 5282, 6051 | append, awk, bash, sed | <h1>Append file contents to the bottom of existing file in Bash</h1>
<blockquote>
<p><strong>Possible Duplicate:</strong><br>
<a href="https://stackoverflow.com/questions/5586293/shell-script-to-append-text-to-each-file">Shell script to append text to each file?</a><br>
<a href="https://stackoverflow.com/question... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 813 | bash | # Append file contents to the bottom of existing file in Bash
> **Possible Duplicate:**
> [Shell script to append text to each file?](https://stackoverflow.com/questions/5586293/shell-script-to-append-text-to-each-file)
> [How to append output to the end of text file in SHELL Script?](https://stackoverflow.com/que... | This should work:
```
cat "$API" >> "$CONFIG"
```
You need to use the `>>` operator to append to a file. Redirecting with `>` causes the file to be overwritten. (truncated). |
7671461 | How do I change the default location for Git Bash on Windows? | 326 | 2011-10-06 07:43:07 | <p>I am using Git on Windows 7 and access my repositories through Git Bash. How can I change the default location that Git Bash opens in a convenient folder when I start it?</p>
<p>It's somewhat time consuming to navigate to <code>htdocs</code>, and then a specific folder. Is there a way to change the configuration fi... | 357,582 | 728,089 | 2024-10-23 01:55:45 | 7,671,508 | 251 | 2011-10-06 07:49:15 | 5,731 | 2017-07-21 16:52:35 | https://stackoverflow.com/q/7671461 | https://stackoverflow.com/a/7671508 | <p>After installing msysgit I have the <code>Git Bash here</code> option in the context menu in Windows Explorer. So I just simply navigate to the directory and then open Bash right there.</p>
<p>I also copied the default <code>Git Bash</code> shortcut to the desktop and edited its <code>Start in</code> property ... | <p>After installing msysgit I have the <code>Git Bash here</code> option in the context menu in Windows Explorer. So I just simply navigate to the directory and then open Bash right there.</p> <p>I also copied the default <code>Git Bash</code> shortcut to the desktop and edited its <code>Start in</code> property ... | 119, 34595, 61874 | git, git-bash, windows-7 | <h1>How do I change the default location for Git Bash on Windows?</h1>
<p>I am using Git on Windows 7 and access my repositories through Git Bash. How can I change the default location that Git Bash opens in a convenient folder when I start it?</p>
<p>It's somewhat time consuming to navigate to <code>htdocs</code>, an... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 814 | bash | # How do I change the default location for Git Bash on Windows?
I am using Git on Windows 7 and access my repositories through Git Bash. How can I change the default location that Git Bash opens in a convenient folder when I start it?
It's somewhat time consuming to navigate to `htdocs`, and then a specific folder. I... | After installing msysgit I have the `Git Bash here` option in the context menu in Windows Explorer. So I just simply navigate to the directory and then open Bash right there.
I also copied the default `Git Bash` shortcut to the desktop and edited its `Start in` property to point to my project directory. It works flawl... |
7330187 | How to find the Windows version from the PowerShell command line | 194 | 2011-09-07 07:20:06 | <p>How do I find which Windows version I'm using?</p>
<p>I'm using PowerShell 2.0 and tried:</p>
<pre><code>PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct a... | 722,907 | 179,748 | 2025-02-20 11:53:20 | 7,330,368 | 251 | 2011-09-07 07:37:56 | 390,278 | 2017-03-02 10:32:31 | https://stackoverflow.com/q/7330187 | https://stackoverflow.com/a/7330368 | <p>Since you have access to the .NET library, you could access the <a href="http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx" rel="noreferrer"><code>OSVersion</code></a> property of the <a href="http://msdn.microsoft.com/en-us/library/system.environment.aspx" rel="noreferrer"><code>System.Envir... | <p>Since you have access to the .NET library, you could access the <a href="http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx" rel="noreferrer"><code>OSVersion</code></a> property of the <a href="http://msdn.microsoft.com/en-us/library/system.environment.aspx" rel="noreferrer"><code>System.Envir... | 64, 526, 24067 | powershell, powershell-2.0, windows | <h1>How to find the Windows version from the PowerShell command line</h1>
<p>How do I find which Windows version I'm using?</p>
<p>I'm using PowerShell 2.0 and tried:</p>
<pre><code>PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 815 | bash | # How to find the Windows version from the PowerShell command line
How do I find which Windows version I'm using?
I'm using PowerShell 2.0 and tried:
```
PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was ... | Since you have access to the .NET library, you could access the [`OSVersion`](http://msdn.microsoft.com/en-us/library/system.environment.osversion.aspx) property of the [`System.Environment`](http://msdn.microsoft.com/en-us/library/system.environment.aspx) class to get this information. For the version number, there is... |
35497069 | Passing IPython variables as arguments to bash commands | 180 | 2016-02-19 04:01:47 | <p>How do I execute a bash command from Ipython/Jupyter notebook passing the value of a python variable as an argument like in this example:</p>
<pre><code>py_var="foo"
!grep py_var bar.txt
</code></pre>
<p>(obviously I want to grep for <code>foo</code> and not the literal string <code>py_var</code>)</p>
| 88,500 | 937,153 | 2025-03-01 22:21:55 | 35,497,161 | 251 | 2016-02-19 04:12:07 | 937,153 | 2025-03-01 22:09:38 | https://stackoverflow.com/q/35497069 | https://stackoverflow.com/a/35497161 | <h2>Simple case</h2>
<p>Prefix your variable names with a <code>$</code>.</p>
<p>For example, say you want to copy a file <code>file1</code> to a path stored in a python variable named <code>dir_path</code>:</p>
<pre><code>dir_path = "/home/foo/bar"
!cp file1 $dir_path
</code></pre>
<ul>
<li><p>Note: If using... | <h2>Simple case</h2> <p>Prefix your variable names with a <code>$</code>.</p> <p>For example, say you want to copy a file <code>file1</code> to a path stored in a python variable named <code>dir_path</code>:</p> <pre><code>dir_path = "/home/foo/bar" !cp file1 $dir_path </code></pre> <ul> <li><p>Note: If using... | 16, 387, 14670, 31134, 116342 | bash, command-line-arguments, ipython, jupyter-notebook, python | <h1>Passing IPython variables as arguments to bash commands</h1>
<p>How do I execute a bash command from Ipython/Jupyter notebook passing the value of a python variable as an argument like in this example:</p>
<pre><code>py_var="foo"
!grep py_var bar.txt
</code></pre>
<p>(obviously I want to grep for <code>foo</code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 816 | bash | # Passing IPython variables as arguments to bash commands
How do I execute a bash command from Ipython/Jupyter notebook passing the value of a python variable as an argument like in this example:
```
py_var="foo"
!grep py_var bar.txt
```
(obviously I want to grep for `foo` and not the literal string `py_var`) | ## Simple case
Prefix your variable names with a `$`.
For example, say you want to copy a file `file1` to a path stored in a python variable named `dir_path`:
```
dir_path = "/home/foo/bar"
!cp file1 $dir_path
```
- Note: If using Bash, keep in mind that [some strings need to be quoted](https://stackoverflow.com/qu... |
2440414 | How can I retrieve the first word of the output of a command in Bash? | 169 | 2010-03-13 23:09:16 | <p>I have a command, for example: <code>echo "word1 word2"</code>. I want to put a pipe (<code>|</code>) and get "word1" from the command.</p>
<pre><code>echo "word1 word2" | ....
</code></pre>
<p>What should I put after the pipe?</p>
| 217,613 | 288,247 | 2026-01-13 12:33:51 | 2,440,521 | 251 | 2010-03-13 23:37:03 | 282,922 | 2021-05-03 15:39:04 | https://stackoverflow.com/q/2440414 | https://stackoverflow.com/a/2440521 | <p><a href="https://en.wikipedia.org/wiki/AWK" rel="noreferrer">AWK</a> is a good option if you have to deal with trailing whitespace because it'll take care of it for you:</p>
<pre><code>echo " word1 word2 " | awk '{print $1;}' # Prints "word1"
</code></pre>
<p><a href="https://en.wikipedia.org/... | <p><a href="https://en.wikipedia.org/wiki/AWK" rel="noreferrer">AWK</a> is a good option if you have to deal with trailing whitespace because it'll take care of it for you:</p> <pre><code>echo " word1 word2 " | awk '{print $1;}' # Prints "word1" </code></pre> <p><a href="https://en.wikipedia.org/... | 387 | bash | <h1>How can I retrieve the first word of the output of a command in Bash?</h1>
<p>I have a command, for example: <code>echo "word1 word2"</code>. I want to put a pipe (<code>|</code>) and get "word1" from the command.</p>
<pre><code>echo "word1 word2" | ....
</code></pre>
<p>What should I ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 817 | bash | # How can I retrieve the first word of the output of a command in Bash?
I have a command, for example: `echo "word1 word2"`. I want to put a pipe (`|`) and get "word1" from the command.
```
echo "word1 word2" | ....
```
What should I put after the pipe? | [AWK](https://en.wikipedia.org/wiki/AWK) is a good option if you have to deal with trailing whitespace because it'll take care of it for you:
```
echo " word1 word2 " | awk '{print $1;}' # Prints "word1"
```
[cut](https://en.wikipedia.org/wiki/Cut_(Unix)) won't take care of this though:
```
echo " word1 word2 "... |
4912733 | How to handle more than 10 parameters in shell | 160 | 2011-02-06 10:26:16 | <p>I am using bash shell on linux and want to use more than 10 parameters in shell script</p>
| 112,172 | 605,164 | 2022-06-26 15:55:43 | 4,912,736 | 251 | 2011-02-06 10:27:23 | 26,428 | 2022-06-26 15:55:43 | https://stackoverflow.com/q/4912733 | https://stackoverflow.com/a/4912736 | <p>Use curly braces to set them off:</p>
<pre><code>echo "${10}"
</code></pre>
<p>Any positional parameter can be saved in a variable to document its use and make later statements more readable:</p>
<pre><code>city_name=${10}
</code></pre>
<p>If fewer parameters are passed then the value at the later position... | <p>Use curly braces to set them off:</p> <pre><code>echo "${10}" </code></pre> <p>Any positional parameter can be saved in a variable to document its use and make later statements more readable:</p> <pre><code>city_name=${10} </code></pre> <p>If fewer parameters are passed then the value at the later position... | 58, 360, 387, 390 | bash, linux, parameters, shell | <h1>How to handle more than 10 parameters in shell</h1>
<p>I am using bash shell on linux and want to use more than 10 parameters in 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 | 818 | bash | # How to handle more than 10 parameters in shell
I am using bash shell on linux and want to use more than 10 parameters in shell script | Use curly braces to set them off:
```
echo "${10}"
```
Any positional parameter can be saved in a variable to document its use and make later statements more readable:
```
city_name=${10}
```
If fewer parameters are passed then the value at the later positions will be unset.
You can also iterate over the positiona... |
670191 | Getting a 'source: not found' error when using source in a bash script | 182 | 2009-03-21 23:14:11 | <p>I'm trying to write (what I thought would be) a simple bash script that will:</p>
<ol>
<li>run virtualenv to create a new environment at $1</li>
<li>activate the virtual environment</li>
<li>do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)</li>
</ol>
<p>Step 1 works quite well,... | 203,046 | 21,245 | 2023-08-16 05:06:43 | 670,216 | 250 | 2009-03-21 23:30:46 | 76,288 | 2009-03-21 23:30:46 | https://stackoverflow.com/q/670191 | https://stackoverflow.com/a/670216 | <p>If you're writing a bash script, call it by name:</p>
<pre><code>#!/bin/bash
</code></pre>
<p>/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).</p>
<p>The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.</p>
| <p>If you're writing a bash script, call it by name:</p> <pre><code>#!/bin/bash </code></pre> <p>/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).</p> <p>The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.</p> | 387, 14529 | bash, virtualenv | <h1>Getting a 'source: not found' error when using source in a bash script</h1>
<p>I'm trying to write (what I thought would be) a simple bash script that will:</p>
<ol>
<li>run virtualenv to create a new environment at $1</li>
<li>activate the virtual environment</li>
<li>do some more stuff (install django, add django... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 819 | bash | # Getting a 'source: not found' error when using source in a bash script
I'm trying to write (what I thought would be) a simple bash script that will:
1. run virtualenv to create a new environment at $1
2. activate the virtual environment
3. do some more stuff (install django, add django-admin.py to the virtualenv's ... | If you're writing a bash script, call it by name:
```
#!/bin/bash
```
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested. |
16497317 | Piping both stdout and stderr in bash? | 258 | 2013-05-11 12:55:20 | <p>It seems that newer versions of bash have the <code>&></code> operator, which (if I understand correctly) <a href="https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-bash">redirects both stdout and stderr</a> to a file (or <code>&>></code> appends to the file).</p>
<p>What's th... | 171,133 | 27,641 | 2025-07-07 16:09:41 | 16,497,456 | 249 | 2013-05-11 13:11:03 | 612,462 | 2025-07-07 15:58:32 | https://stackoverflow.com/q/16497317 | https://stackoverflow.com/a/16497456 | <p>To combine stdout and stderr you would redirect stderr to stdout using <code>2>&1</code>:</p>
<pre><code>$ { echo "stdout"; echo "stderr" 1>&2; } 2>&1 | grep -v std
$
</code></pre>
<p>You can read more about redirection in the Bash manual: <a href="https://www.gnu.org/softwar... | <p>To combine stdout and stderr you would redirect stderr to stdout using <code>2>&1</code>:</p> <pre><code>$ { echo "stdout"; echo "stderr" 1>&2; } 2>&1 | grep -v std $ </code></pre> <p>You can read more about redirection in the Bash manual: <a href="https://www.gnu.org/softwar... | 387, 4867, 5813, 19156 | bash, pipe, stderr, stdout | <h1>Piping both stdout and stderr in bash?</h1>
<p>It seems that newer versions of bash have the <code>&></code> operator, which (if I understand correctly) <a href="https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-bash">redirects both stdout and stderr</a> to a file (or <code>&>&g... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 820 | bash | # Piping both stdout and stderr in bash?
It seems that newer versions of bash have the `&>` operator, which (if I understand correctly) [redirects both stdout and stderr](https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-bash) to a file (or `&>>` appends to the file).
What's the simplest way to... | To combine stdout and stderr you would redirect stderr to stdout using `2>&1`:
```
$ { echo "stdout"; echo "stderr" 1>&2; } 2>&1 | grep -v std
$
```
You can read more about redirection in the Bash manual: [Redirections](https://www.gnu.org/software/bash/manual/html_node/Redirections.html)
### To elaborate
This comm... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.