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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3777301 | How can I call a shell script from Python code? | 316 | 2010-09-23 10:12:57 | <p>How can I call a shell script from Python code?</p>
| 607,280 | 449,355 | 2025-05-23 18:05:38 | 3,777,308 | 385 | 2010-09-23 10:13:45 | 140,185 | 2025-05-23 17:18:48 | https://stackoverflow.com/q/3777301 | https://stackoverflow.com/a/3777308 | <p>The <a href="http://docs.python.org/library/subprocess.html" rel="nofollow noreferrer">subprocess</a> module will help you out.</p>
<p>Blatantly trivial example:</p>
<pre><code>>>> import subprocess
>>> subprocess.call(['sh', './test.sh']) # Thanks @Jim Dennis for suggesting the []
0
</code></pre>... | <p>The <a href="http://docs.python.org/library/subprocess.html" rel="nofollow noreferrer">subprocess</a> module will help you out.</p> <p>Blatantly trivial example:</p> <pre><code>>>> import subprocess >>> subprocess.call(['sh', './test.sh']) # Thanks @Jim Dennis for suggesting the [] 0 </code></pre>... | 16, 390 | python, shell | <h1>How can I call a shell script from Python code?</h1>
<p>How can I call a shell script from Python code?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 519 | bash | # How can I call a shell script from Python code?
How can I call a shell script from Python code? | The [subprocess](http://docs.python.org/library/subprocess.html) module will help you out.
Blatantly trivial example:
```
>>> import subprocess
>>> subprocess.call(['sh', './test.sh']) # Thanks @Jim Dennis for suggesting the []
0
```
Where `test.sh` is a simple shell script and `0` is its return value for this run. |
1298066 | How can I check if a package is installed and install it if not? | 317 | 2009-08-19 06:18:50 | <p>I'm working on a Ubuntu system and currently this is what I'm doing:</p>
<pre class="lang-none prettyprint-override"><code>if ! which command > /dev/null; then
echo -e "Command not found! Install? (y/n) \c"
read
if "$REPLY" = "y"; then
sudo apt-get install command
f... | 373,558 | 100,758 | 2024-10-25 08:23:19 | 1,298,103 | 384 | 2009-08-19 06:27:47 | 73,603 | 2024-10-11 21:37:02 | https://stackoverflow.com/q/1298066 | https://stackoverflow.com/a/1298103 | <p>To check if <code>packagename</code> was installed, type:</p>
<pre><code>dpkg -s <packagename>
</code></pre>
<p>You can also use <code>dpkg-query</code> that has a neater output for your purpose, and accepts wild cards, too.</p>
<pre><code>dpkg-query -l <packagename>
</code></pre>
<p>To find what package... | <p>To check if <code>packagename</code> was installed, type:</p> <pre><code>dpkg -s <packagename> </code></pre> <p>You can also use <code>dpkg-query</code> that has a neater output for your purpose, and accepts wild cards, too.</p> <pre><code>dpkg-query -l <packagename> </code></pre> <p>To find what package... | 387, 16113 | apt-get, bash | <h1>How can I check if a package is installed and install it if not?</h1>
<p>I'm working on a Ubuntu system and currently this is what I'm doing:</p>
<pre class="lang-none prettyprint-override"><code>if ! which command > /dev/null; then
echo -e "Command not found! Install? (y/n) \c"
read
if "... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 520 | bash | # How can I check if a package is installed and install it if not?
I'm working on a Ubuntu system and currently this is what I'm doing:
```
if ! which command > /dev/null; then
echo -e "Command not found! Install? (y/n) \c"
read
if "$REPLY" = "y"; then
sudo apt-get install command
fi
fi
```
Is this... | To check if `packagename` was installed, type:
```
dpkg -s <packagename>
```
You can also use `dpkg-query` that has a neater output for your purpose, and accepts wild cards, too.
```
dpkg-query -l <packagename>
```
To find what package owns the `command`, try:
```
dpkg -S `which <command>`
```
You can use these i... |
13356628 | How to redirect the output of the time command to a file in Linux? | 293 | 2012-11-13 06:54:12 | <p>Just a little question about timing programs on Linux: the time command allows to
measure the execution time of a program:</p>
<pre><code>[ed@lbox200 ~]$ time sleep 1
real 0m1.004s
user 0m0.000s
sys 0m0.004s
</code></pre>
<p>Which works fine. But if I try to redirect the output to a file, it fails.</p>
... | 167,890 | 1,528,443 | 2021-01-20 00:29:49 | 13,356,654 | 384 | 2012-11-13 06:56:53 | 1,686,814 | 2016-05-11 19:56:19 | https://stackoverflow.com/q/13356628 | https://stackoverflow.com/a/13356654 | <p>Try</p>
<pre><code>{ time sleep 1 ; } 2> time.txt
</code></pre>
<p>which combines the STDERR of "time" and your command into time.txt</p>
<p>Or use</p>
<pre><code>{ time sleep 1 2> sleep.stderr ; } 2> time.txt
</code></pre>
<p>which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR ... | <p>Try</p> <pre><code>{ time sleep 1 ; } 2> time.txt </code></pre> <p>which combines the STDERR of "time" and your command into time.txt</p> <p>Or use</p> <pre><code>{ time sleep 1 2> sleep.stderr ; } 2> time.txt </code></pre> <p>which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR ... | 58, 387, 603 | bash, linux, time | <h1>How to redirect the output of the time command to a file in Linux?</h1>
<p>Just a little question about timing programs on Linux: the time command allows to
measure the execution time of a program:</p>
<pre><code>[ed@lbox200 ~]$ time sleep 1
real 0m1.004s
user 0m0.000s
sys 0m0.004s
</code></pre>
<p>Whi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 521 | bash | # How to redirect the output of the time command to a file in Linux?
Just a little question about timing programs on Linux: the time command allows to
measure the execution time of a program:
```
[ed@lbox200 ~]$ time sleep 1
real 0m1.004s
user 0m0.000s
sys 0m0.004s
```
Which works fine. But if I try to re... | Try
```
{ time sleep 1 ; } 2> time.txt
```
which combines the STDERR of "time" and your command into time.txt
Or use
```
{ time sleep 1 2> sleep.stderr ; } 2> time.txt
```
which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR from "time" goes into "time.txt" |
35006457 | choosing between $0 and BASH_SOURCE | 274 | 2016-01-26 03:10:42 | <p>How does one choose between <code>"$0"</code> and <code>"${BASH_SOURCE[0]}"</code></p>
<p>This description from <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables" rel="noreferrer">GNU</a> didn't help me much.</p>
<pre class="lang-none prettyprint-overr... | 189,647 | 427,155 | 2024-05-24 11:25:52 | 35,006,505 | 383 | 2016-01-26 03:15:17 | 45,375 | 2024-05-24 11:25:52 | https://stackoverflow.com/q/35006457 | https://stackoverflow.com/a/35006505 | <p><sup>Note: For a <em>POSIX-compliant</em> solution, see <a href="https://stackoverflow.com/a/29835459/45375">this answer</a>.</sup></p>
<p><code>${BASH_SOURCE[0]}</code> (or, more simply, <code>$BASH_SOURCE</code><sup>[1]</sup>) contains the (potentially relative) path of the containing script in <em>all</em> invoca... | <p><sup>Note: For a <em>POSIX-compliant</em> solution, see <a href="https://stackoverflow.com/a/29835459/45375">this answer</a>.</sup></p> <p><code>${BASH_SOURCE[0]}</code> (or, more simply, <code>$BASH_SOURCE</code><sup>[1]</sup>) contains the (potentially relative) path of the containing script in <em>all</em> invoca... | 387 | bash | <h1>choosing between $0 and BASH_SOURCE</h1>
<p>How does one choose between <code>"$0"</code> and <code>"${BASH_SOURCE[0]}"</code></p>
<p>This description from <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables" rel="noreferrer">GNU</a> didn't help me much... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 522 | bash | # choosing between $0 and BASH_SOURCE
How does one choose between `"$0"` and `"${BASH_SOURCE[0]}"`
This description from [GNU](https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#Bash-Variables) didn't help me much.
```
BASH_SOURCE
An array variable whose members are the source filenames... | Note: For a *POSIX-compliant* solution, see [this answer](https://stackoverflow.com/a/29835459/45375).
`${BASH_SOURCE[0]}` (or, more simply, `$BASH_SOURCE`[1]) contains the (potentially relative) path of the containing script in *all* invocation scenarios, notably also when the script is *sourced*, which is not true f... |
92802 | What is the Linux equivalent to DOS pause? | 285 | 2008-09-18 14:02:27 | <p>I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the <code>pause</code> command. Is there a Linux equivalent I can use in my script?</p>
| 210,470 | 4,362 | 2023-09-04 10:58:34 | 92,813 | 382 | 2008-09-18 14:03:44 | 8,427 | 2017-03-19 09:19:31 | https://stackoverflow.com/q/92802 | https://stackoverflow.com/a/92813 | <p><code>read</code> does this:</p>
<pre><code>user@host:~$ read -n1 -r -p "Press any key to continue..." key
[...]
user@host:~$
</code></pre>
<p>The <code>-n1</code> specifies that it only waits for a single character. The <code>-r</code> puts it into raw mode, which is necessary because otherwise, if you press som... | <p><code>read</code> does this:</p> <pre><code>user@host:~$ read -n1 -r -p "Press any key to continue..." key [...] user@host:~$ </code></pre> <p>The <code>-n1</code> specifies that it only waits for a single character. The <code>-r</code> puts it into raw mode, which is necessary because otherwise, if you press som... | 58, 387, 390 | bash, linux, shell | <h1>What is the Linux equivalent to DOS pause?</h1>
<p>I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the <code>pause</code> command. Is there a Linux equivalent I can use in my script?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 523 | bash | # What is the Linux equivalent to DOS pause?
I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the `pause` command. Is there a Linux equivalent I can use in my script? | `read` does this:
```
user@host:~$ read -n1 -r -p "Press any key to continue..." key
[...]
user@host:~$
```
The `-n1` specifies that it only waits for a single character. The `-r` puts it into raw mode, which is necessary because otherwise, if you press something like backslash, it doesn't register until you hit the ... |
2227182 | How can I find out a file's MIME type (Content-Type)? | 194 | 2010-02-09 06:20:20 | <p>Is there a way to find out the MIME type (or is it called "Content-Type"?) of a file in a Linux bash script?</p>
<p>The reason I need it is because ImageShack appears to need it to upload a file, as for some reason it detects the .png file as an <code>application/octet-stream</code> file.</p>
<p>I’ve check... | 208,909 | 138,541 | 2025-09-09 06:50:53 | 2,227,201 | 382 | 2010-02-09 06:25:22 | 195,802 | 2016-03-01 12:39:35 | https://stackoverflow.com/q/2227182 | https://stackoverflow.com/a/2227201 | <p>Use <code>file</code>. Examples:</p>
<pre><code>> file --mime-type image.png
image.png: image/png
> file -b --mime-type image.png
image/png
> file -i FILE_NAME
image.png: image/png; charset=binary
</code></pre>
| <p>Use <code>file</code>. Examples:</p> <pre><code>> file --mime-type image.png image.png: image/png > file -b --mime-type image.png image/png > file -i FILE_NAME image.png: image/png; charset=binary </code></pre> | 58, 387, 1492, 2983, 59645 | bash, content-type, linux, mime, mime-types | <h1>How can I find out a file's MIME type (Content-Type)?</h1>
<p>Is there a way to find out the MIME type (or is it called "Content-Type"?) of a file in a Linux bash script?</p>
<p>The reason I need it is because ImageShack appears to need it to upload a file, as for some reason it detects the .png file as a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 524 | bash | # How can I find out a file's MIME type (Content-Type)?
Is there a way to find out the MIME type (or is it called "Content-Type"?) of a file in a Linux bash script?
The reason I need it is because ImageShack appears to need it to upload a file, as for some reason it detects the .png file as an `application/octet-stre... | Use `file`. Examples:
```
> file --mime-type image.png
image.png: image/png
> file -b --mime-type image.png
image/png
> file -i FILE_NAME
image.png: image/png; charset=binary
``` |
25044010 | Running cURL on 64 bit Windows | 142 | 2014-07-30 18:12:55 | <p>I'm new to cURL, just got it installed but it seems to only do what it feels like. I'm using the 64 bit version I got from here: <a href="http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi">http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi</a> with installation instructions I found here: <a href="http://guides.ins... | 126,401 | 2,013,212 | 2024-07-07 21:32:56 | 25,045,154 | 382 | 2014-07-30 19:19:18 | 608,772 | 2020-05-07 06:22:45 | https://stackoverflow.com/q/25044010 | https://stackoverflow.com/a/25045154 | <p>Your problem is that your are not using the <code>Curl</code> you installed but a CmdLet called <code>Invoke-WebRequest</code>.</p>
<p>Just execute :</p>
<pre><code>Remove-item alias:curl
</code></pre>
<p>And test your curl again, then store it in your profile.</p>
<p>The explanation is that it exists a native a... | <p>Your problem is that your are not using the <code>Curl</code> you installed but a CmdLet called <code>Invoke-WebRequest</code>.</p> <p>Just execute :</p> <pre><code>Remove-item alias:curl </code></pre> <p>And test your curl again, then store it in your profile.</p> <p>The explanation is that it exists a native a... | 64, 526, 1554 | curl, powershell, windows | <h1>Running cURL on 64 bit Windows</h1>
<p>I'm new to cURL, just got it installed but it seems to only do what it feels like. I'm using the 64 bit version I got from here: <a href="http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi">http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi</a> with installation instructions ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 525 | bash | # Running cURL on 64 bit Windows
I'm new to cURL, just got it installed but it seems to only do what it feels like. I'm using the 64 bit version I got from here: <http://curl.haxx.se/latest.cgi?curl=win64-ssl-sspi> with installation instructions I found here: <http://guides.instructure.com/m/4214/l/83393-how-do-i-inst... | Your problem is that your are not using the `Curl` you installed but a CmdLet called `Invoke-WebRequest`.
Just execute :
```
Remove-item alias:curl
```
And test your curl again, then store it in your profile.
The explanation is that it exists a native alias to the `Invoke-WebRequest` which is a CmdLet that is suppo... |
11693074 | git: 'credential-cache' is not a git command | 333 | 2012-07-27 17:52:25 | <p>I followed <a href="https://help.github.com/articles/set-up-git" rel="noreferrer">these instructions</a> to the letter, including the part about password caching. It seems like the instructions are wrong, because every time I <code>git push origin master</code> I get this error:</p>
<pre><code>git: 'credential-cach... | 304,287 | 1,015,595 | 2023-05-08 18:17:57 | 11,889,392 | 381 | 2012-08-09 18:06:20 | 89,590 | 2021-07-16 19:29:09 | https://stackoverflow.com/q/11693074 | https://stackoverflow.com/a/11889392 | <p>From <a href="http://blog.sdbarker.com/2012/07/09/git-credential-caching-on-windows/" rel="noreferrer">a blog I found</a>:</p>
<blockquote>
<p>This [git-credential-cache] doesn’t work for Windows systems as git-credential-cache communicates through a Unix socket.</p>
</blockquote>
<p><strong>Git for Windows</strong>... | <p>From <a href="http://blog.sdbarker.com/2012/07/09/git-credential-caching-on-windows/" rel="noreferrer">a blog I found</a>:</p> <blockquote> <p>This [git-credential-cache] doesn’t work for Windows systems as git-credential-cache communicates through a Unix socket.</p> </blockquote> <p><strong>Git for Windows</strong>... | 119, 21628, 32868, 61874 | git, git-bash, git-config, msysgit | <h1>git: 'credential-cache' is not a git command</h1>
<p>I followed <a href="https://help.github.com/articles/set-up-git" rel="noreferrer">these instructions</a> to the letter, including the part about password caching. It seems like the instructions are wrong, because every time I <code>git push origin master</code> I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 526 | bash | # git: 'credential-cache' is not a git command
I followed [these instructions](https://help.github.com/articles/set-up-git) to the letter, including the part about password caching. It seems like the instructions are wrong, because every time I `git push origin master` I get this error:
```
git: 'credential-cache' is... | From [a blog I found](http://blog.sdbarker.com/2012/07/09/git-credential-caching-on-windows/):
> This [git-credential-cache] doesn’t work for Windows systems as git-credential-cache communicates through a Unix socket.
**Git for Windows**
Since msysgit has been superseded by Git for Windows, using [Git for Windows](h... |
23391839 | Clone private git repo with dockerfile | 386 | 2014-04-30 15:14:10 | <p>I have copied this code from what seems to be various working dockerfiles around, here is mine:</p>
<pre><code>FROM ubuntu
MAINTAINER Luke Crooks "luke@pumalo.org"
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git python-virtualenv
# Make ssh dir
RUN mkdir /root/.... | 466,462 | 2,508,665 | 2023-10-07 09:05:30 | 23,411,161 | 380 | 2014-05-01 15:43:21 | 2,508,665 | 2019-01-10 04:49:41 | https://stackoverflow.com/q/23391839 | https://stackoverflow.com/a/23411161 | <p>My key was password protected which was causing the problem, a working file is now listed below (for help of future googlers)</p>
<pre><code>FROM ubuntu
MAINTAINER Luke Crooks "luke@pumalo.org"
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git
# Make ssh dir
RUN mk... | <p>My key was password protected which was causing the problem, a working file is now listed below (for help of future googlers)</p> <pre><code>FROM ubuntu MAINTAINER Luke Crooks "luke@pumalo.org" # Update aptitude with new repo RUN apt-get update # Install software RUN apt-get install -y git # Make ssh dir RUN mk... | 119, 387, 90304 | bash, docker, git | <h1>Clone private git repo with dockerfile</h1>
<p>I have copied this code from what seems to be various working dockerfiles around, here is mine:</p>
<pre><code>FROM ubuntu
MAINTAINER Luke Crooks "luke@pumalo.org"
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git pyt... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 527 | bash | # Clone private git repo with dockerfile
I have copied this code from what seems to be various working dockerfiles around, here is mine:
```
FROM ubuntu
MAINTAINER Luke Crooks "luke@pumalo.org"
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git python-virtualenv
# Ma... | My key was password protected which was causing the problem, a working file is now listed below (for help of future googlers)
```
FROM ubuntu
MAINTAINER Luke Crooks "luke@pumalo.org"
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git
# Make ssh dir
RUN mkdir /root/.ssh... |
2462385 | Getting an "ambiguous redirect" error | 251 | 2010-03-17 13:08:32 | <p>The following line in my Bash script</p>
<pre><code> echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
</code></pre>
<p>gives me this error:</p>
<pre><code> line 46: ${OUPUT_RESULTS}: ambiguous redirect
</code></pre>
<p>Why?</p>
| 505,390 | 171,546 | 2023-12-14 22:29:53 | 7,775,870 | 379 | 2011-10-15 05:03:09 | 86,967 | 2014-08-30 06:52:36 | https://stackoverflow.com/q/2462385 | https://stackoverflow.com/a/7775870 | <p>Bash can be pretty obtuse sometimes.</p>
<p>The following commands all return different error messages for basically the same error:</p>
<pre><code>$ echo hello >
bash: syntax error near unexpected token `newline`
$ echo hello > ${NONEXISTENT}
bash: ${NONEXISTENT}: ambiguous redirect
$ echo hello > "${N... | <p>Bash can be pretty obtuse sometimes.</p> <p>The following commands all return different error messages for basically the same error:</p> <pre><code>$ echo hello > bash: syntax error near unexpected token `newline` $ echo hello > ${NONEXISTENT} bash: ${NONEXISTENT}: ambiguous redirect $ echo hello > "${N... | 387 | bash | <h1>Getting an "ambiguous redirect" error</h1>
<p>The following line in my Bash script</p>
<pre><code> echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
</code></pre>
<p>gives me this error:</p>
<pre><code> line 46: ${OUPUT_RESULTS}: ambiguous redirect
</code></pre>
<p>Why?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 528 | bash | # Getting an "ambiguous redirect" error
The following line in my Bash script
```
echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
```
gives me this error:
```
line 46: ${OUPUT_RESULTS}: ambiguous redirect
```
Why? | Bash can be pretty obtuse sometimes.
The following commands all return different error messages for basically the same error:
```
$ echo hello >
bash: syntax error near unexpected token `newline`
$ echo hello > ${NONEXISTENT}
bash: ${NONEXISTENT}: ambiguous redirect
$ echo hello > "${NONEXISTENT}"
bash: : No such f... |
7901517 | How to redirect stderr and stdout to different files in the same line in script? | 234 | 2011-10-26 10:36:52 | <p>I know this much:</p>
<pre><code>$ command 2>> error
$ command 1>> output
</code></pre>
<p>Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?</p>
| 137,703 | 784,637 | 2020-05-17 11:33:20 | 7,901,533 | 379 | 2011-10-26 10:38:46 | 67,373 | 2011-10-26 10:45:31 | https://stackoverflow.com/q/7901517 | https://stackoverflow.com/a/7901533 | <p>Just add them in one line <code>command 2>> error 1>> output</code></p>
<p>However, note that <code>>></code> is for appending if the file already has data. Whereas, <code>></code> will overwrite any existing data in the file.</p>
<p>So, <code>command 2> error 1> output</code> if you do ... | <p>Just add them in one line <code>command 2>> error 1>> output</code></p> <p>However, note that <code>>></code> is for appending if the file already has data. Whereas, <code>></code> will overwrite any existing data in the file.</p> <p>So, <code>command 2> error 1> output</code> if you do ... | 58, 387 | bash, linux | <h1>How to redirect stderr and stdout to different files in the same line in script?</h1>
<p>I know this much:</p>
<pre><code>$ command 2>> error
$ command 1>> output
</code></pre>
<p>Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 529 | bash | # How to redirect stderr and stdout to different files in the same line in script?
I know this much:
```
$ command 2>> error
$ command 1>> output
```
Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash? | Just add them in one line `command 2>> error 1>> output`
However, note that `>>` is for appending if the file already has data. Whereas, `>` will overwrite any existing data in the file.
So, `command 2> error 1> output` if you do not want to append.
Just for completion's sake, you can write `1>` as just `>` since th... |
56784894 | macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell? | 150 | 2019-06-27 06:08:50 | <p>I want to set the environment variable I added below the line to <code>~/.bash_profile</code> and <code>~/.profile</code> but it didn't work.</p>
<pre><code>export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
</code></pre>
<p>Afterward, exit the terminal and open it again when executing <code>echo $JBOSS_HOME</code... | 107,560 | 5,440,666 | 2022-05-15 17:52:18 | 58,310,864 | 378 | 2019-10-09 19:17:21 | 9,151,379 | 2019-10-09 19:17:21 | https://stackoverflow.com/q/56784894 | https://stackoverflow.com/a/58310864 | <p>Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. <code>.bashrc</code> is now <code>.zshrc</code> and <code>.bash_profile</code> is now <code>.zprofile</code>.</p>
| <p>Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. <code>.bashrc</code> is now <code>.zshrc</code> and <code>.bash_profile</code> is now <code>.zprofile</code>.</p> | 387, 390, 3791, 9013, 138656 | bash, environment-variables, macos-catalina, shell, zsh | <h1>macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell?</h1>
<p>I want to set the environment variable I added below the line to <code>~/.bash_profile</code> and <code>~/.profile</code> but it didn't work.</p>
<pre><code>export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
</code></pre>
<p>Afte... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 530 | bash | # macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell?
I want to set the environment variable I added below the line to `~/.bash_profile` and `~/.profile` but it didn't work.
```
export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
```
Afterward, exit the terminal and open it again when executi... | Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. `.bashrc` is now `.zshrc` and `.bash_profile` is now `.zprofile`. |
16854280 | A variable modified inside a while loop is not remembered | 307 | 2013-05-31 09:38:24 | <p>In the following program, if I set the variable <code>$foo</code> to the value 1 inside the first <code>if</code> statement, it works in the sense that its value is remembered after the if statement. However, when I set the same variable to the value 2 inside an <code>if</code> which is inside a <code>while</code> s... | 285,354 | 712,089 | 2025-06-19 18:10:51 | 16,854,326 | 377 | 2013-05-31 09:40:30 | 1,275,169 | 2019-01-02 21:19:24 | https://stackoverflow.com/q/16854280 | https://stackoverflow.com/a/16854326 | <pre><code>echo -e $lines | while read line
...
done
</code></pre>
<p>The <code>while</code> loop is executed in a subshell. So any changes you do to the variable will not be available once the subshell exits.</p>
<p>Instead you can use a <a href="https://www.gnu.org/software/bash/manual/bashref.html#Here-String... | <pre><code>echo -e $lines | while read line ... done </code></pre> <p>The <code>while</code> loop is executed in a subshell. So any changes you do to the variable will not be available once the subshell exits.</p> <p>Instead you can use a <a href="https://www.gnu.org/software/bash/manual/bashref.html#Here-String... | 387, 2182, 10327, 17310 | bash, scope, sh, while-loop | <h1>A variable modified inside a while loop is not remembered</h1>
<p>In the following program, if I set the variable <code>$foo</code> to the value 1 inside the first <code>if</code> statement, it works in the sense that its value is remembered after the if statement. However, when I set the same variable to the value... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 531 | bash | # A variable modified inside a while loop is not remembered
In the following program, if I set the variable `$foo` to the value 1 inside the first `if` statement, it works in the sense that its value is remembered after the if statement. However, when I set the same variable to the value 2 inside an `if` which is insi... | ```
echo -e $lines | while read line
...
done
```
The `while` loop is executed in a subshell. So any changes you do to the variable will not be available once the subshell exits.
Instead you can use a [*here string*](https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings) to re-write the while loop t... |
6338015 | How do you execute an arbitrary native command from a string? | 245 | 2011-06-14 00:54:59 | <p>I can express my need with the following scenario: <strong>Write a function that accepts a string to be run as a native command.</strong> </p>
<p>It's not too far fetched of an idea: if you're interfacing with other command-line utilities from elsewhere in the company that supply you with a command to run verbatim.... | 325,120 | 791,838 | 2019-11-21 14:05:10 | 6,338,047 | 377 | 2011-06-14 01:00:08 | 22,211 | 2018-12-13 11:50:50 | https://stackoverflow.com/q/6338015 | https://stackoverflow.com/a/6338047 | <p><code>Invoke-Expression</code>, also aliased as <code>iex</code>. The following will work on your examples #2 and #3:</p>
<pre><code>iex $command
</code></pre>
<p>Some strings won't run as-is, such as your example #1 because the exe is in quotes. This will work as-is, because the contents of the string are exactly... | <p><code>Invoke-Expression</code>, also aliased as <code>iex</code>. The following will work on your examples #2 and #3:</p> <pre><code>iex $command </code></pre> <p>Some strings won't run as-is, such as your example #1 because the exe is in quotes. This will work as-is, because the contents of the string are exactly... | 526, 531 | powershell, scripting | <h1>How do you execute an arbitrary native command from a string?</h1>
<p>I can express my need with the following scenario: <strong>Write a function that accepts a string to be run as a native command.</strong> </p>
<p>It's not too far fetched of an idea: if you're interfacing with other command-line utilities from e... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 532 | bash | # How do you execute an arbitrary native command from a string?
I can express my need with the following scenario: **Write a function that accepts a string to be run as a native command.**
It's not too far fetched of an idea: if you're interfacing with other command-line utilities from elsewhere in the company that s... | `Invoke-Expression`, also aliased as `iex`. The following will work on your examples #2 and #3:
```
iex $command
```
Some strings won't run as-is, such as your example #1 because the exe is in quotes. This will work as-is, because the contents of the string are exactly how you would run it straight from a Powershell ... |
3134791 | How do I remove newlines from a text file? | 211 | 2010-06-28 17:46:03 | <p>I have the following data, and I need to put it all into one line.</p>
<p>I have this:</p>
<pre><code>22791
;
14336
;
22821
;
34653
;
21491
;
25522
;
33238
;
</code></pre>
<p>I need this:</p>
<pre><code>22791;14336;22821;34653;21491;25522;33238;
</code></pre>
<hr />
<h1>EDIT</h1>
<p>None of these comman... | 491,406 | 368,453 | 2023-12-31 17:09:15 | 3,134,827 | 377 | 2010-06-28 17:51:46 | 39,375 | 2023-12-31 17:08:14 | https://stackoverflow.com/q/3134791 | https://stackoverflow.com/a/3134827 | <pre class="lang-none prettyprint-override"><code>tr --delete '\n' < yourfile.txt
tr -d '\n' < yourfile.txt
</code></pre>
<p>If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I woul... | <pre class="lang-none prettyprint-override"><code>tr --delete '\n' < yourfile.txt tr -d '\n' < yourfile.txt </code></pre> <p>If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I woul... | 58, 387, 390, 531, 5282 | bash, linux, scripting, sed, shell | <h1>How do I remove newlines from a text file?</h1>
<p>I have the following data, and I need to put it all into one line.</p>
<p>I have this:</p>
<pre><code>22791
;
14336
;
22821
;
34653
;
21491
;
25522
;
33238
;
</code></pre>
<p>I need this:</p>
<pre><code>22791;14336;22821;34653;21491;25522;33238;
</code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 533 | bash | # How do I remove newlines from a text file?
I have the following data, and I need to put it all into one line.
I have this:
```
22791
;
14336
;
22821
;
34653
;
21491
;
25522
;
33238
;
```
I need this:
```
22791;14336;22821;34653;21491;25522;33238;
```
---
# EDIT
None of these commands is working p... | ```
tr --delete '\n' < yourfile.txt
tr -d '\n' < yourfile.txt
```
If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I would expect the Perl solutions to work even in that case)?
Try:
`... |
11111562 | rsync copy over only certain types of files using include option | 226 | 2012-06-20 01:09:36 | <p>I use the following bash script to copy only files of certain extension(in this case *.sh), however it still copies over all the files. what's wrong?</p>
<pre>
from=$1
to=$2
rsync -zarv --include="*.sh" $from $to
</pre>
| 206,080 | 881,480 | 2025-04-24 20:08:38 | 11,111,793 | 374 | 2012-06-20 01:48:42 | 1,126,841 | 2025-01-24 14:23:59 | https://stackoverflow.com/q/11111562 | https://stackoverflow.com/a/11111793 | <p>I think <code>--include</code> is used to include a subset of files that are otherwise excluded by <code>--exclude</code>, rather than including only those files.
In other words: you have to think about <em>include</em> meaning <strong>don't exclude</strong>.</p>
<p>Try instead:</p>
<pre><code>rsync -zarv --include=... | <p>I think <code>--include</code> is used to include a subset of files that are otherwise excluded by <code>--exclude</code>, rather than including only those files. In other words: you have to think about <em>include</em> meaning <strong>don't exclude</strong>.</p> <p>Try instead:</p> <pre><code>rsync -zarv --include=... | 58, 387, 390, 1159 | bash, linux, rsync, shell | <h1>rsync copy over only certain types of files using include option</h1>
<p>I use the following bash script to copy only files of certain extension(in this case *.sh), however it still copies over all the files. what's wrong?</p>
<pre>
from=$1
to=$2
rsync -zarv --include="*.sh" $from $to
</pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 534 | bash | # rsync copy over only certain types of files using include option
I use the following bash script to copy only files of certain extension(in this case *.sh), however it still copies over all the files. what's wrong?
```
from=$1
to=$2
rsync -zarv --include="*.sh" $from $to
``` | I think `--include` is used to include a subset of files that are otherwise excluded by `--exclude`, rather than including only those files.
In other words: you have to think about *include* meaning **don't exclude**.
Try instead:
```
rsync -zarv --include="*/" --include="*.sh" --exclude="*" "$from" "$to"
```
For rs... |
8364640 | How to properly handle a gzipped page when using curl? | 201 | 2011-12-03 01:05:48 | <p>I wrote a bash script that gets output from a website using curl and does a bunch of string manipulation on the html output. The problem is when I run it against a site that is returning its output gzipped. Going to the site in a browser works fine. </p>
<p>When I run curl by hand, I get gzipped output:</p>
<pre><... | 158,102 | 41,688 | 2021-12-09 10:58:35 | 8,365,089 | 374 | 2011-12-03 02:47:57 | 119,159 | 2011-12-03 02:47:57 | https://stackoverflow.com/q/8364640 | https://stackoverflow.com/a/8365089 | <p><code>curl</code> will automatically decompress the response if you set the <code>--compressed</code> flag:</p>
<pre><code>curl --compressed "http://example.com"
</code></pre>
<blockquote>
<p><strong>--compressed</strong>
(HTTP) Request a compressed response using one of the algorithms libcurl supports, and... | <p><code>curl</code> will automatically decompress the response if you set the <code>--compressed</code> flag:</p> <pre><code>curl --compressed "http://example.com" </code></pre> <blockquote> <p><strong>--compressed</strong> (HTTP) Request a compressed response using one of the algorithms libcurl supports, and... | 387, 1554, 1930 | bash, curl, gzip | <h1>How to properly handle a gzipped page when using curl?</h1>
<p>I wrote a bash script that gets output from a website using curl and does a bunch of string manipulation on the html output. The problem is when I run it against a site that is returning its output gzipped. Going to the site in a browser works fine. </p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 535 | bash | # How to properly handle a gzipped page when using curl?
I wrote a bash script that gets output from a website using curl and does a bunch of string manipulation on the html output. The problem is when I run it against a site that is returning its output gzipped. Going to the site in a browser works fine.
When I run ... | `curl` will automatically decompress the response if you set the `--compressed` flag:
```
curl --compressed "http://example.com"
```
> **--compressed**
> (HTTP) Request a compressed response using one of the algorithms libcurl supports, and save the uncompressed document. If this option is used and the server sends a... |
9242163 | Bash mkdir and subfolders | 195 | 2012-02-11 17:07:56 | <p>Why I can't do something like this? <code>mkdir folder/subfolder/</code> in order to achive this I have to do: </p>
<pre><code>mkdir folder
cd folder
mkdir subfolder
</code></pre>
<p>Is there a better way to do it?</p>
| 215,544 | 58,839 | 2016-04-29 11:48:50 | 9,242,171 | 374 | 2012-02-11 17:09:08 | 1,172,428 | 2012-02-11 17:09:08 | https://stackoverflow.com/q/9242163 | https://stackoverflow.com/a/9242171 | <p>You can:</p>
<pre><code>mkdir -p folder/subfolder
</code></pre>
<p>The <code>-p</code> flag causes any parent directories to be created if necessary.</p>
| <p>You can:</p> <pre><code>mkdir -p folder/subfolder </code></pre> <p>The <code>-p</code> flag causes any parent directories to be created if necessary.</p> | 387, 24423, 61874 | bash, git-bash, mkdir | <h1>Bash mkdir and subfolders</h1>
<p>Why I can't do something like this? <code>mkdir folder/subfolder/</code> in order to achive this I have to do: </p>
<pre><code>mkdir folder
cd folder
mkdir subfolder
</code></pre>
<p>Is there a better way to do it?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 536 | bash | # Bash mkdir and subfolders
Why I can't do something like this? `mkdir folder/subfolder/` in order to achive this I have to do:
```
mkdir folder
cd folder
mkdir subfolder
```
Is there a better way to do it? | You can:
```
mkdir -p folder/subfolder
```
The `-p` flag causes any parent directories to be created if necessary. |
6541109 | Send string to stdin | 242 | 2011-06-30 21:07:10 | <p>Is there a way to effectively do this in bash:</p>
<pre><code>/my/bash/script < echo 'This string will be sent to stdin.'
</code></pre>
<p>I'm aware that I could pipe the output from the echo such as this:</p>
<pre><code>echo 'This string will be piped to stdin.' | /my/bash/script
</code></pre>
| 281,300 | 369,450 | 2024-10-10 00:42:42 | 6,541,324 | 372 | 2011-06-30 21:30:24 | 632,407 | 2011-06-30 21:30:24 | https://stackoverflow.com/q/6541109 | https://stackoverflow.com/a/6541324 | <p>You can use one-line <code>heredoc</code></p>
<pre><code>cat <<< "This is coming from the stdin"
</code></pre>
<p>the above is the same as</p>
<pre><code>cat <<EOF
This is coming from the stdin
EOF
</code></pre>
<p>or you can redirect output from a command, like</p>
<pre><code>diff <(ls /bin) ... | <p>You can use one-line <code>heredoc</code></p> <pre><code>cat <<< "This is coming from the stdin" </code></pre> <p>the above is the same as</p> <pre><code>cat <<EOF This is coming from the stdin EOF </code></pre> <p>or you can redirect output from a command, like</p> <pre><code>diff <(ls /bin) ... | 387, 1701, 26698, 31901 | bash, http-redirect, io-redirection, stdin | <h1>Send string to stdin</h1>
<p>Is there a way to effectively do this in bash:</p>
<pre><code>/my/bash/script < echo 'This string will be sent to stdin.'
</code></pre>
<p>I'm aware that I could pipe the output from the echo such as this:</p>
<pre><code>echo 'This string will be piped to stdin.' | /my/bash/script... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 537 | bash | # Send string to stdin
Is there a way to effectively do this in bash:
```
/my/bash/script < echo 'This string will be sent to stdin.'
```
I'm aware that I could pipe the output from the echo such as this:
```
echo 'This string will be piped to stdin.' | /my/bash/script
``` | You can use one-line `heredoc`
```
cat <<< "This is coming from the stdin"
```
the above is the same as
```
cat <<EOF
This is coming from the stdin
EOF
```
or you can redirect output from a command, like
```
diff <(ls /bin) <(ls /usr/bin)
```
or you can read as
```
while read line
do
echo =$line=
done < some_... |
18135451 | What is the difference between ${var}, "$var", and "${var}" in the Bash shell? | 218 | 2013-08-08 20:25:19 | <p>What the title says: what does it mean to encapsulate a variable in <code>{}</code>, <code>""</code>, or <code>"{}</code>"? I haven't been able to find any explanations online about this - I haven't been able to refer to them except for using the symbols, which doesn't yield anything.</p>
<p>Here's an example:</p>
... | 143,003 | 2,336,976 | 2023-02-20 04:40:55 | 18,136,920 | 372 | 2013-08-08 22:02:26 | 176,646 | 2013-08-09 16:16:06 | https://stackoverflow.com/q/18135451 | https://stackoverflow.com/a/18136920 | <h2>Braces (<code>$var</code> vs. <code>${var}</code>)</h2>
<p>In most cases, <code>$var</code> and <code>${var}</code> are the same:</p>
<pre><code>var=foo
echo $var
# foo
echo ${var}
# foo
</code></pre>
<p>The braces are only needed to resolve ambiguity in expressions:</p>
<pre><code>var=foo
echo $varbar
# Prints... | <h2>Braces (<code>$var</code> vs. <code>${var}</code>)</h2> <p>In most cases, <code>$var</code> and <code>${var}</code> are the same:</p> <pre><code>var=foo echo $var # foo echo ${var} # foo </code></pre> <p>The braces are only needed to resolve ambiguity in expressions:</p> <pre><code>var=foo echo $varbar # Prints... | 276, 367, 387, 390 | bash, shell, syntax, variables | <h1>What is the difference between ${var}, "$var", and "${var}" in the Bash shell?</h1>
<p>What the title says: what does it mean to encapsulate a variable in <code>{}</code>, <code>""</code>, or <code>"{}</code>"? I haven't been able to find any explanations online about this - I haven't been able to refer to them exc... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 538 | bash | # What is the difference between ${var}, "$var", and "${var}" in the Bash shell?
What the title says: what does it mean to encapsulate a variable in `{}`, `""`, or `"{}`"? I haven't been able to find any explanations online about this - I haven't been able to refer to them except for using the symbols, which doesn't y... | ## Braces (`$var` vs. `${var}`)
In most cases, `$var` and `${var}` are the same:
```
var=foo
echo $var
# foo
echo ${var}
# foo
```
The braces are only needed to resolve ambiguity in expressions:
```
var=foo
echo $varbar
# Prints nothing because there is no variable 'varbar'
echo ${var}bar
# foobar
```
## Quotes (`... |
7103531 | How to get the part of a file after the first line that matches a regular expression | 210 | 2011-08-18 06:57:39 | <p>I have a file with about 1000 lines. I want the part of my file after the line which matches my grep statement.</p>
<p>That is:</p>
<pre><code>cat file | grep 'TERMINATE' # It is found on line 534
</code></pre>
<p>So, I want the file from line 535 to line 1000 for further processing.</p>
<p>How can I do that?</p... | 239,521 | 731,963 | 2023-03-14 13:12:38 | 7,104,422 | 372 | 2011-08-18 08:23:59 | 851,677 | 2021-10-29 16:01:43 | https://stackoverflow.com/q/7103531 | https://stackoverflow.com/a/7104422 | <p>The following will print the line matching <code>TERMINATE</code> till the end of the file:</p>
<pre><code>sed -n -e '/TERMINATE/,$p'
</code></pre>
<p><strong>Explained:</strong> <code>-n</code> disables default behavior of <code>sed</code> of printing each line after executing its script on it, <code>-e</code> indi... | <p>The following will print the line matching <code>TERMINATE</code> till the end of the file:</p> <pre><code>sed -n -e '/TERMINATE/,$p' </code></pre> <p><strong>Explained:</strong> <code>-n</code> disables default behavior of <code>sed</code> of printing each line after executing its script on it, <code>-e</code> indi... | 387, 390, 531, 1271 | bash, grep, scripting, shell | <h1>How to get the part of a file after the first line that matches a regular expression</h1>
<p>I have a file with about 1000 lines. I want the part of my file after the line which matches my grep statement.</p>
<p>That is:</p>
<pre><code>cat file | grep 'TERMINATE' # It is found on line 534
</code></pre>
<p>So, I... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 539 | bash | # How to get the part of a file after the first line that matches a regular expression
I have a file with about 1000 lines. I want the part of my file after the line which matches my grep statement.
That is:
```
cat file | grep 'TERMINATE' # It is found on line 534
```
So, I want the file from line 535 to line ... | The following will print the line matching `TERMINATE` till the end of the file:
```
sed -n -e '/TERMINATE/,$p'
```
**Explained:** `-n` disables default behavior of `sed` of printing each line after executing its script on it, `-e` indicated a script to `sed`, `/TERMINATE/,$` is an address (line) range selection mean... |
19335004 | How to run a PowerShell script from a batch file | 278 | 2013-10-12 13:57:42 | <p>I am trying to run this script in PowerShell. I have saved the below script as <code>ps.ps1</code> on my desktop.</p>
<pre><code>$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}
</code></pre>
<p>I have made a batc... | 1,312,148 | 996,366 | 2025-05-01 18:20:14 | 19,335,763 | 370 | 2013-10-12 15:17:42 | 73,070 | 2013-10-12 15:17:42 | https://stackoverflow.com/q/19335004 | https://stackoverflow.com/a/19335763 | <p>You need the <code>-ExecutionPolicy</code> parameter:</p>
<pre><code>Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
</code></pre>
<p>Otherwise PowerShell considers the arguments a line to execute and while <code>Set-ExecutionPolicy</code> <em>is</em> a cmdlet, it has no <code>-File<... | <p>You need the <code>-ExecutionPolicy</code> parameter:</p> <pre><code>Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1 </code></pre> <p>Otherwise PowerShell considers the arguments a line to execute and while <code>Set-ExecutionPolicy</code> <em>is</em> a cmdlet, it has no <code>-File<... | 64, 390, 526, 7002 | batch-file, powershell, shell, windows | <h1>How to run a PowerShell script from a batch file</h1>
<p>I am trying to run this script in PowerShell. I have saved the below script as <code>ps.ps1</code> on my desktop.</p>
<pre><code>$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Pr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 540 | bash | # How to run a PowerShell script from a batch file
I am trying to run this script in PowerShell. I have saved the below script as `ps.ps1` on my desktop.
```
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}
```
I ha... | You need the `-ExecutionPolicy` parameter:
```
Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
```
Otherwise PowerShell considers the arguments a line to execute and while `Set-ExecutionPolicy` *is* a cmdlet, it has no `-File` parameter. |
7723584 | PowerShell: How do I convert an array object to a string in PowerShell? | 232 | 2011-10-11 08:58:34 | <p>How can I convert an array object to string?</p>
<p>I tried:</p>
<pre class="lang-sh prettyprint-override"><code>$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
</code></pre>
<p><strike>with no luck</strike>. What are different possibilities in PowerShell?</p>
| 726,743 | 179,748 | 2021-08-04 16:39:56 | 7,723,859 | 367 | 2011-10-11 09:23:27 | 323,582 | 2014-11-12 19:46:24 | https://stackoverflow.com/q/7723584 | https://stackoverflow.com/a/7723859 |
<pre class="lang-sh prettyprint-override"><code>$a = 'This', 'Is', 'a', 'cat'
</code></pre>
<p>Using double quotes (and optionally use the separator <code>$ofs</code>)</p>
<pre class="lang-sh prettyprint-override"><code># This Is a cat
"$a"
# This-Is-a-cat
$ofs = '-' # after this all casts work this way until $ofs... | <pre class="lang-sh prettyprint-override"><code>$a = 'This', 'Is', 'a', 'cat' </code></pre> <p>Using double quotes (and optionally use the separator <code>$ofs</code>)</p> <pre class="lang-sh prettyprint-override"><code># This Is a cat "$a" # This-Is-a-cat $ofs = '-' # after this all casts work this way until $ofs... | 526, 24067 | powershell, powershell-2.0 | <h1>PowerShell: How do I convert an array object to a string in PowerShell?</h1>
<p>How can I convert an array object to string?</p>
<p>I tried:</p>
<pre class="lang-sh prettyprint-override"><code>$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
</code></pre>
<p><strike>with no luck</strike>. What are di... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 541 | bash | # PowerShell: How do I convert an array object to a string in PowerShell?
How can I convert an array object to string?
I tried:
```
$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)
```
with no luck. What are different possibilities in PowerShell? | ```
$a = 'This', 'Is', 'a', 'cat'
```
Using double quotes (and optionally use the separator `$ofs`)
```
# This Is a cat
"$a"
# This-Is-a-cat
$ofs = '-' # after this all casts work this way until $ofs changes!
"$a"
```
Using operator join
```
# This-Is-a-cat
$a -join '-'
# ThisIsacat
-join $a
```
Using conversion... |
34571711 | Can't run Curl command inside my Docker Container | 194 | 2016-01-02 23:13:38 | <p>I created a docker container from my OS X VM Docker host. I created it using the run command and created the container based off the <code>ubuntu:xenial</code> image off docker hub.</p>
<p>I'm now connected to my container after it's created and logged in as root and at the command prompt inside my container.</p>
... | 489,985 | 4,694,781 | 2024-11-13 07:01:37 | 34,571,766 | 367 | 2016-01-02 23:22:13 | 465,183 | 2023-01-11 07:34:20 | https://stackoverflow.com/q/34571711 | https://stackoverflow.com/a/34571766 | <blockquote>
<p>curl: command not found</p>
</blockquote>
<p>is a big hint, you have to install it with :</p>
<pre><code>apt-get -y update; apt-get -y install curl
</code></pre>
| <blockquote> <p>curl: command not found</p> </blockquote> <p>is a big hint, you have to install it with :</p> <pre><code>apt-get -y update; apt-get -y install curl </code></pre> | 390, 529, 90304 | docker, installation, shell | <h1>Can't run Curl command inside my Docker Container</h1>
<p>I created a docker container from my OS X VM Docker host. I created it using the run command and created the container based off the <code>ubuntu:xenial</code> image off docker hub.</p>
<p>I'm now connected to my container after it's created and logged in ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 542 | bash | # Can't run Curl command inside my Docker Container
I created a docker container from my OS X VM Docker host. I created it using the run command and created the container based off the `ubuntu:xenial` image off docker hub.
I'm now connected to my container after it's created and logged in as root and at the command p... | > curl: command not found
is a big hint, you have to install it with :
```
apt-get -y update; apt-get -y install curl
``` |
2696055 | Intersection of two lists in Bash | 210 | 2010-04-23 03:37:14 | <p>I'm trying to write a simple script that will list the contents found in two lists. To simplify, let's use <em>ls</em> as an example. Imagine "one" and "two" are directories.</p>
<pre>
one=`ls one`
two=`ls two`
intersection $one $two
</pre>
<p>I'm still quite green in Bash, so feel free to corr... | 95,449 | 125,380 | 2023-03-27 12:18:55 | 2,696,111 | 366 | 2010-04-23 03:58:01 | 131,527 | 2010-04-23 03:58:01 | https://stackoverflow.com/q/2696055 | https://stackoverflow.com/a/2696111 | <pre><code>comm -12 <(ls 1) <(ls 2)
</code></pre>
| <pre><code>comm -12 <(ls 1) <(ls 2) </code></pre> | 387 | bash | <h1>Intersection of two lists in Bash</h1>
<p>I'm trying to write a simple script that will list the contents found in two lists. To simplify, let's use <em>ls</em> as an example. Imagine "one" and "two" are directories.</p>
<pre>
one=`ls one`
two=`ls two`
intersection $one $two
</pre>
<p>I'm stil... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 543 | bash | # Intersection of two lists in Bash
I'm trying to write a simple script that will list the contents found in two lists. To simplify, let's use *ls* as an example. Imagine "one" and "two" are directories.
```
one=`ls one`
two=`ls two`
intersection $one $two
```
I'm still quite green in Bash, so feel free to correct h... | ```
comm -12 <(ls 1) <(ls 2)
``` |
762348 | How can I exclude all "permission denied" messages from "find"? | 1,035 | 2009-04-17 21:54:07 | <p>I need to hide all <em>permission denied</em> messages from:</p>
<pre><code>find . > files_and_folders
</code></pre>
<p>I am experimenting when such message arises. I need to gather all folders and files, to which it does not arise. </p>
<p>Is it possible to direct the permission levels to the <code>files_and_... | 782,814 | 54,964 | 2025-02-01 19:55:26 | 40,336,333 | 365 | 2016-10-31 03:51:28 | 45,375 | 2025-02-01 19:55:26 | https://stackoverflow.com/q/762348 | https://stackoverflow.com/a/40336333 | <p>Note:</p>
<ul>
<li>This answer probably goes deeper than the use case warrants, and <strong><code>find 2>/dev/null</code> may be good enough in many situations</strong>. It may still be of interest for a cross-platform perspective and for its discussion of some advanced shell techniques in the interest of finding... | <p>Note:</p> <ul> <li>This answer probably goes deeper than the use case warrants, and <strong><code>find 2>/dev/null</code> may be good enough in many situations</strong>. It may still be of interest for a cross-platform perspective and for its discussion of some advanced shell techniques in the interest of finding... | 379, 387, 2120, 10193 | bash, error-handling, file-permissions, find | <h1>How can I exclude all "permission denied" messages from "find"?</h1>
<p>I need to hide all <em>permission denied</em> messages from:</p>
<pre><code>find . > files_and_folders
</code></pre>
<p>I am experimenting when such message arises. I need to gather all folders and files, to which it does not arise. </p>
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 544 | bash | # How can I exclude all "permission denied" messages from "find"?
I need to hide all *permission denied* messages from:
```
find . > files_and_folders
```
I am experimenting when such message arises. I need to gather all folders and files, to which it does not arise.
Is it possible to direct the permission levels t... | Note:
- This answer probably goes deeper than the use case warrants, and **`find 2>/dev/null` may be good enough in many situations**. It may still be of interest for a cross-platform perspective and for its discussion of some advanced shell techniques in the interest of finding a solution that is as robust as possibl... |
7780030 | How to fix Terminal not loading ~/.bashrc on OS X Lion | 183 | 2011-10-15 19:15:16 | <p>Whenever I open a new tab in Terminal using <kbd>Cmd</kbd> + <kbd>T</kbd>, it opens bash in the same directory, as the previous tab. This works fine when I'm in the <code>~</code> directory, but if I'm anywhere else, I get an error loading <code>.bashrc</code></p>
<pre><code>Last login: Sat Oct 15 21:10:00 on ttys0... | 107,015 | 72,583 | 2016-05-12 08:42:52 | 7,780,055 | 365 | 2011-10-15 19:19:23 | 785,998 | 2016-05-12 08:42:52 | https://stackoverflow.com/q/7780030 | https://stackoverflow.com/a/7780055 | <p>Terminal opens a login shell. This means, <code>~/.bash_profile</code> will get executed, <code>~/.bashrc</code> not.</p>
<p>The solution on most systems is to "require" the <code>~/.bashrc</code> in the <code>~/.bash_profile</code>: just put this snippet in your <code>~/.bash_profile</code>:</p>
<pre><code>[[ -s ... | <p>Terminal opens a login shell. This means, <code>~/.bash_profile</code> will get executed, <code>~/.bashrc</code> not.</p> <p>The solution on most systems is to "require" the <code>~/.bashrc</code> in the <code>~/.bash_profile</code>: just put this snippet in your <code>~/.bash_profile</code>:</p> <pre><code>[[ -s ... | 369, 387, 391, 71219 | bash, macos, osx-lion, terminal | <h1>How to fix Terminal not loading ~/.bashrc on OS X Lion</h1>
<p>Whenever I open a new tab in Terminal using <kbd>Cmd</kbd> + <kbd>T</kbd>, it opens bash in the same directory, as the previous tab. This works fine when I'm in the <code>~</code> directory, but if I'm anywhere else, I get an error loading <code>.bashrc... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 545 | bash | # How to fix Terminal not loading ~/.bashrc on OS X Lion
Whenever I open a new tab in Terminal using `Cmd` + `T`, it opens bash in the same directory, as the previous tab. This works fine when I'm in the `~` directory, but if I'm anywhere else, I get an error loading `.bashrc`
```
Last login: Sat Oct 15 21:10:00 on t... | Terminal opens a login shell. This means, `~/.bash_profile` will get executed, `~/.bashrc` not.
The solution on most systems is to "require" the `~/.bashrc` in the `~/.bash_profile`: just put this snippet in your `~/.bash_profile`:
```
[[ -s ~/.bashrc ]] && source ~/.bashrc
``` |
49110 | How do I write a 'for' loop in Bash? | 334 | 2008-09-08 03:10:28 | <p>I'm looking for the basic loop like:</p>
<pre class="lang-bash prettyprint-override"><code>for (int i = 0; i < MAX; i++) {
doSomething(i);
}
</code></pre>
<p>but for <code>bash</code>.</p>
| 649,541 | 3,535 | 2025-09-04 22:37:34 | 49,111 | 364 | 2008-09-08 03:11:07 | 4,798 | 2025-03-02 21:42:12 | https://stackoverflow.com/q/49110 | https://stackoverflow.com/a/49111 | <p>From <a href="https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html" rel="noreferrer">this site</a>:</p>
<pre><code>for i in $(seq 1 10);
do
echo $i
done
</code></pre>
| <p>From <a href="https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html" rel="noreferrer">this site</a>:</p> <pre><code>for i in $(seq 1 10); do echo $i done </code></pre> | 387, 2097, 2531 | bash, for-loop, iterator | <h1>How do I write a 'for' loop in Bash?</h1>
<p>I'm looking for the basic loop like:</p>
<pre class="lang-bash prettyprint-override"><code>for (int i = 0; i < MAX; i++) {
doSomething(i);
}
</code></pre>
<p>but for <code>bash</code>.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 546 | bash | # How do I write a 'for' loop in Bash?
I'm looking for the basic loop like:
```
for (int i = 0; i < MAX; i++) {
doSomething(i);
}
```
but for `bash`. | From [this site](https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html):
```
for i in $(seq 1 10);
do
echo $i
done
``` |
12722095 | How do I use floating-point arithmetic in bash? | 397 | 2012-10-04 07:16:56 | <p>I am trying to divide two image widths in a Bash script, but bash gives me <code>0</code> as the result:</p>
<pre><code>RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))
</code></pre>
<p>I did study the Bash guide and I know I should use <code>bc</code>, in all examples in internet they use <code>bc</code>. In <code>echo</code> ... | 556,617 | 1,433,240 | 2025-09-19 03:57:43 | 12,722,107 | 363 | 2012-10-04 07:18:10 | 20,862 | 2012-10-04 07:18:10 | https://stackoverflow.com/q/12722095 | https://stackoverflow.com/a/12722107 | <p>You can't. bash <em>only</em> does integers; you <em>must</em> delegate to a tool such as <code>bc</code>.</p>
| <p>You can't. bash <em>only</em> does integers; you <em>must</em> delegate to a tool such as <code>bc</code>.</p> | 305, 387, 5481 | arithmetic-expressions, bash, floating-point | <h1>How do I use floating-point arithmetic in bash?</h1>
<p>I am trying to divide two image widths in a Bash script, but bash gives me <code>0</code> as the result:</p>
<pre><code>RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))
</code></pre>
<p>I did study the Bash guide and I know I should use <code>bc</code>, in all examples in... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 547 | bash | # How do I use floating-point arithmetic in bash?
I am trying to divide two image widths in a Bash script, but bash gives me `0` as the result:
```
RESULT=$(($IMG_WIDTH/$IMG2_WIDTH))
```
I did study the Bash guide and I know I should use `bc`, in all examples in internet they use `bc`. In `echo` I tried to put the s... | You can't. bash *only* does integers; you *must* delegate to a tool such as `bc`. |
18488651 | How to break out of a loop in Bash? | 271 | 2013-08-28 12:59:10 | <p>I want to write a Bash script to process text, which might require a while loop.</p>
<p>For example, a while loop in C:</p>
<pre class="lang-c prettyprint-override"><code>int done = 0;
while(1) {
...
if(done) break;
}
</code></pre>
<p>I want to write a Bash script equivalent to that. But what I usually used and ... | 392,565 | 1,607,051 | 2025-02-28 13:29:06 | 18,488,730 | 363 | 2013-08-28 13:02:36 | 1,126,841 | 2020-11-06 12:42:49 | https://stackoverflow.com/q/18488651 | https://stackoverflow.com/a/18488730 | <p>It's not that different in <code>bash</code>.</p>
<pre><code>workdone=0
while : ; do
...
if [ "$workdone" -ne 0 ]; then
break
fi
done
</code></pre>
<p><code>:</code> is the no-op command; its exit status is always 0, so the loop runs until <code>workdone</code> is given a non-zero value.</p>
<h... | <p>It's not that different in <code>bash</code>.</p> <pre><code>workdone=0 while : ; do ... if [ "$workdone" -ne 0 ]; then break fi done </code></pre> <p><code>:</code> is the no-op command; its exit status is always 0, so the loop runs until <code>workdone</code> is given a non-zero value.</p> <h... | 387, 390, 17310 | bash, shell, while-loop | <h1>How to break out of a loop in Bash?</h1>
<p>I want to write a Bash script to process text, which might require a while loop.</p>
<p>For example, a while loop in C:</p>
<pre class="lang-c prettyprint-override"><code>int done = 0;
while(1) {
...
if(done) break;
}
</code></pre>
<p>I want to write a Bash script equ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 548 | bash | # How to break out of a loop in Bash?
I want to write a Bash script to process text, which might require a while loop.
For example, a while loop in C:
```
int done = 0;
while(1) {
...
if(done) break;
}
```
I want to write a Bash script equivalent to that. But what I usually used and as all the classic examples ... | It's not that different in `bash`.
```
workdone=0
while : ; do
...
if [ "$workdone" -ne 0 ]; then
break
fi
done
```
`:` is the no-op command; its exit status is always 0, so the loop runs until `workdone` is given a non-zero value.
---
There are many ways you could set and test the value of `workdone` i... |
3545292 | How to get Maven project version to the bash command line | 303 | 2010-08-23 06:53:31 | <p>Previous I issued a question on <a href="https://stackoverflow.com/questions/3519005/update-a-maven-project-version-from-script">how to change Maven project vesion from command line</a> which lead me to a new issue.</p>
<p>Previously I was able to get the version number since the version was stored as a property tha... | 250,438 | 194,350 | 2024-12-06 08:06:01 | 3,545,363 | 362 | 2010-08-23 07:06:01 | 70,604 | 2022-05-25 11:54:31 | https://stackoverflow.com/q/3545292 | https://stackoverflow.com/a/3545363 | <p>The <a href="http://maven.apache.org/plugins/maven-help-plugin/" rel="noreferrer">Maven Help Plugin</a> is somehow already proposing something for this:</p>
<blockquote>
<ul>
<li><a href="http://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html" rel="noreferrer"><code>help:evaluate</code></a> evaluates M... | <p>The <a href="http://maven.apache.org/plugins/maven-help-plugin/" rel="noreferrer">Maven Help Plugin</a> is somehow already proposing something for this:</p> <blockquote> <ul> <li><a href="http://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html" rel="noreferrer"><code>help:evaluate</code></a> evaluates M... | 387, 1231, 1912 | bash, command-line, maven-2 | <h1>How to get Maven project version to the bash command line</h1>
<p>Previous I issued a question on <a href="https://stackoverflow.com/questions/3519005/update-a-maven-project-version-from-script">how to change Maven project vesion from command line</a> which lead me to a new issue.</p>
<p>Previously I was able to ge... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 549 | bash | # How to get Maven project version to the bash command line
Previous I issued a question on [how to change Maven project vesion from command line](https://stackoverflow.com/questions/3519005/update-a-maven-project-version-from-script) which lead me to a new issue.
Previously I was able to get the version number since... | The [Maven Help Plugin](http://maven.apache.org/plugins/maven-help-plugin/) is somehow already proposing something for this:
> - [`help:evaluate`](http://maven.apache.org/plugins/maven-help-plugin/evaluate-mojo.html) evaluates Maven expressions given by the user in an interactive mode.
Here is how you would invoke it... |
1537673 | How do I forward parameters to other command in bash script? | 265 | 2009-10-08 13:05:15 | <p>Inside my bash script, I would like to parse zero, one or two parameters (the script can recognize them), then forward the remaining parameters to a command invoked in the script. How can I do that?</p>
| 131,399 | 61,342 | 2020-04-07 18:08:26 | 1,537,695 | 362 | 2009-10-08 13:09:12 | 28,169 | 2017-04-29 14:45:37 | https://stackoverflow.com/q/1537673 | https://stackoverflow.com/a/1537695 | <p>Use the <a href="http://www.gnu.org/software/bash/manual/bashref.html#index-shift" rel="noreferrer"><code>shift</code></a> built-in command to "eat" the arguments. Then call the child process and pass it the <a href="http://www.gnu.org/software/bash/manual/bashref.html#index-_0040" rel="noreferrer"><code>"$@"</code>... | <p>Use the <a href="http://www.gnu.org/software/bash/manual/bashref.html#index-shift" rel="noreferrer"><code>shift</code></a> built-in command to "eat" the arguments. Then call the child process and pass it the <a href="http://www.gnu.org/software/bash/manual/bashref.html#index-_0040" rel="noreferrer"><code>"$@"</code>... | 387, 1231 | bash, command-line | <h1>How do I forward parameters to other command in bash script?</h1>
<p>Inside my bash script, I would like to parse zero, one or two parameters (the script can recognize them), then forward the remaining parameters to a command invoked in the script. How can 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 | 550 | bash | # How do I forward parameters to other command in bash script?
Inside my bash script, I would like to parse zero, one or two parameters (the script can recognize them), then forward the remaining parameters to a command invoked in the script. How can I do that? | Use the [`shift`](http://www.gnu.org/software/bash/manual/bashref.html#index-shift) built-in command to "eat" the arguments. Then call the child process and pass it the [`"$@"`](http://www.gnu.org/software/bash/manual/bashref.html#index-_0040) argument to include all remaining arguments. Notice the quotes, they should ... |
2355148 | Run a string as a command within a Bash script | 201 | 2010-03-01 10:36:22 | <p>I have a Bash script that builds a string to run as a command</p>
<p><strong>Script:</strong></p>
<pre><code>#! /bin/bash
matchdir="/home/joao/robocup/runner_workdir/matches/testmatch/"
teamAComm="`pwd`/a.sh"
teamBComm="`pwd`/b.sh"
include="`pwd`/server_official.conf"
serverbin='/usr/local/bin/rcssserver'
cd $m... | 539,996 | 86,845 | 2023-09-18 12:48:42 | 2,355,242 | 362 | 2010-03-01 10:52:19 | 12,890 | 2023-09-18 12:48:42 | https://stackoverflow.com/q/2355148 | https://stackoverflow.com/a/2355242 | <p>You can use <code>eval</code> to execute a string:</p>
<pre><code>eval $illcommando
</code></pre>
<p>If your command string needs to be evaluated itself before it is ran - wrap in quotes:</p>
<pre><code>eval "$yourcommand"
# e.g. eval "command argument --your-option='$(date -d "$date" +%Y-%... | <p>You can use <code>eval</code> to execute a string:</p> <pre><code>eval $illcommando </code></pre> <p>If your command string needs to be evaluated itself before it is ran - wrap in quotes:</p> <pre><code>eval "$yourcommand" # e.g. eval "command argument --your-option='$(date -d "$date" +%Y-%... | 387, 390, 31134 | bash, command-line-arguments, shell | <h1>Run a string as a command within a Bash script</h1>
<p>I have a Bash script that builds a string to run as a command</p>
<p><strong>Script:</strong></p>
<pre><code>#! /bin/bash
matchdir="/home/joao/robocup/runner_workdir/matches/testmatch/"
teamAComm="`pwd`/a.sh"
teamBComm="`pwd`/b.sh"
include="`pwd`/server_off... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 551 | bash | # Run a string as a command within a Bash script
I have a Bash script that builds a string to run as a command
**Script:**
```
#! /bin/bash
matchdir="/home/joao/robocup/runner_workdir/matches/testmatch/"
teamAComm="`pwd`/a.sh"
teamBComm="`pwd`/b.sh"
include="`pwd`/server_official.conf"
serverbin='/usr/local/bin/rc... | You can use `eval` to execute a string:
```
eval $illcommando
```
If your command string needs to be evaluated itself before it is ran - wrap in quotes:
```
eval "$yourcommand"
# e.g. eval "command argument --your-option='$(date -d "$date" +%Y-%m-%d)'"
``` |
4316730 | Hiding user input on terminal in Linux script | 168 | 2010-11-30 17:44:59 | <p>I have bash script like the following:</p>
<pre><code>#!/bin/bash
echo "Please enter your username";
read username;
echo "Please enter your password";
read password;
</code></pre>
<p>I want that when the user types the password on the terminal, it should not be displayed (or something like *******) should be di... | 153,596 | null | 2025-07-16 15:30:54 | 4,316,747 | 362 | 2010-11-30 17:46:14 | 135,448 | 2010-11-30 17:46:14 | https://stackoverflow.com/q/4316730 | https://stackoverflow.com/a/4316747 | <p>Just supply -s to your read call like so:</p>
<pre><code>$ read -s PASSWORD
$ echo $PASSWORD
</code></pre>
| <p>Just supply -s to your read call like so:</p> <pre><code>$ read -s PASSWORD $ echo $PASSWORD </code></pre> | 58, 387, 531 | bash, linux, scripting | <h1>Hiding user input on terminal in Linux script</h1>
<p>I have bash script like the following:</p>
<pre><code>#!/bin/bash
echo "Please enter your username";
read username;
echo "Please enter your password";
read password;
</code></pre>
<p>I want that when the user types the password on the terminal, it should not... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 552 | bash | # Hiding user input on terminal in Linux script
I have bash script like the following:
```
#!/bin/bash
echo "Please enter your username";
read username;
echo "Please enter your password";
read password;
```
I want that when the user types the password on the terminal, it should not be displayed (or something like ... | Just supply -s to your read call like so:
```
$ read -s PASSWORD
$ echo $PASSWORD
``` |
402377 | Using getopts to process long and short command line options | 562 | 2008-12-31 05:49:05 | <p>I would like to have the long and short forms of command line options invoked using my shell script.</p>
<p>I know that <code>getopts</code> can be used, like in Perl, but I have not been able to implement the same using shell.</p>
<p>Any ideas on how this can be done, so that I can use options like:</p>
<pre><code>... | 633,225 | 35,416 | 2025-05-26 16:34:44 | 402,410 | 360 | 2008-12-31 06:27:41 | 20,860 | 2020-03-26 16:49:23 | https://stackoverflow.com/q/402377 | https://stackoverflow.com/a/402410 | <p>There are three implementations that may be considered:</p>
<ul>
<li><p>Bash builtin <code>getopts</code>. This does not support long option names with the double-dash prefix. It only supports single-character options.</p></li>
<li><p>BSD UNIX implementation of standalone <code>getopt</code> command (which is what... | <p>There are three implementations that may be considered:</p> <ul> <li><p>Bash builtin <code>getopts</code>. This does not support long option names with the double-dash prefix. It only supports single-character options.</p></li> <li><p>BSD UNIX implementation of standalone <code>getopt</code> command (which is what... | 387, 10831, 19020, 31134 | bash, command-line-arguments, getopt, getopts | <h1>Using getopts to process long and short command line options</h1>
<p>I would like to have the long and short forms of command line options invoked using my shell script.</p>
<p>I know that <code>getopts</code> can be used, like in Perl, but I have not been able to implement the same using shell.</p>
<p>Any ideas on... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 553 | bash | # Using getopts to process long and short command line options
I would like to have the long and short forms of command line options invoked using my shell script.
I know that `getopts` can be used, like in Perl, but I have not been able to implement the same using shell.
Any ideas on how this can be done, so that I... | There are three implementations that may be considered:
- Bash builtin `getopts`. This does not support long option names with the double-dash prefix. It only supports single-character options.
- BSD UNIX implementation of standalone `getopt` command (which is what MacOS uses). This does not support long options eithe... |
6331075 | Why do you need ./ (dot-slash) before executable or script name to run it in Bash? | 357 | 2011-06-13 13:28:01 | <p>When running scripts in Bash, I have to write <code>./</code> in the beginning:</p>
<pre class="lang-none prettyprint-override"><code>./manage.py syncdb
</code></pre>
<p>If I don't, I get an error message:</p>
<pre class="lang-none prettyprint-override"><code>manage.py syncdb
</code></pre>
<p>Output:</p>
<blockquote... | 131,259 | 458,193 | 2025-12-02 07:57:34 | 6,331,085 | 360 | 2011-06-13 13:29:05 | 714,501 | 2025-01-31 18:26:01 | https://stackoverflow.com/q/6331075 | https://stackoverflow.com/a/6331085 | <p>Because on Unix, usually, the current directory is not in <code>$PATH</code>.</p>
<p>When you type a command the shell looks up a list of directories, as specified by the <code>PATH</code> variable.; the current directory is not in that list.</p>
<p>The reason for not having the current directory on that list is sec... | <p>Because on Unix, usually, the current directory is not in <code>$PATH</code>.</p> <p>When you type a command the shell looks up a list of directories, as specified by the <code>PATH</code> variable.; the current directory is not in that list.</p> <p>The reason for not having the current directory on that list is sec... | 34, 387, 390, 1231 | bash, command-line, shell, unix | <h1>Why do you need ./ (dot-slash) before executable or script name to run it in Bash?</h1>
<p>When running scripts in Bash, I have to write <code>./</code> in the beginning:</p>
<pre class="lang-none prettyprint-override"><code>./manage.py syncdb
</code></pre>
<p>If I don't, I get an error message:</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 | 554 | bash | # Why do you need ./ (dot-slash) before executable or script name to run it in Bash?
When running scripts in Bash, I have to write `./` in the beginning:
```
./manage.py syncdb
```
If I don't, I get an error message:
```
manage.py syncdb
```
Output:
> -bash: manage.py: command not found
What is the reason for th... | Because on Unix, usually, the current directory is not in `$PATH`.
When you type a command the shell looks up a list of directories, as specified by the `PATH` variable.; the current directory is not in that list.
The reason for not having the current directory on that list is security.
Let's say you're root and go ... |
3806874 | How to merge two files line by line in Bash | 233 | 2010-09-27 18:55:46 | <p>I have two text files, each of them contains an information by line such like that</p>
<pre><code>file1.txt file2.txt
---------- ---------
linef11 linef21
linef12 linef22
linef13 linef23
. .
. .
. ... | 194,516 | 409,723 | 2022-05-01 18:27:55 | 3,806,901 | 360 | 2010-09-27 18:58:52 | 61,974 | 2010-09-27 18:58:52 | https://stackoverflow.com/q/3806874 | https://stackoverflow.com/a/3806901 | <p>You can use <a href="http://ss64.com/bash/paste.html" rel="noreferrer"><code>paste</code></a>:</p>
<pre><code>paste file1.txt file2.txt > fileresults.txt
</code></pre>
| <p>You can use <a href="http://ss64.com/bash/paste.html" rel="noreferrer"><code>paste</code></a>:</p> <pre><code>paste file1.txt file2.txt > fileresults.txt </code></pre> | 34, 387 | bash, unix | <h1>How to merge two files line by line in Bash</h1>
<p>I have two text files, each of them contains an information by line such like that</p>
<pre><code>file1.txt file2.txt
---------- ---------
linef11 linef21
linef12 linef22
linef13 linef23
. ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 555 | bash | # How to merge two files line by line in Bash
I have two text files, each of them contains an information by line such like that
```
file1.txt file2.txt
---------- ---------
linef11 linef21
linef12 linef22
linef13 linef23
. .
. ... | You can use [`paste`](http://ss64.com/bash/paste.html):
```
paste file1.txt file2.txt > fileresults.txt
``` |
16489809 | Emulating a do-while loop in Bash | 240 | 2013-05-10 19:49:00 | <p>What is the best way to emulate a do-while loop in Bash?</p>
<p>I could check for the condition before entering the <code>while</code> loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way?</p>
<p>Pseudo code of my script:</p>
<pre><code>while [ current_... | 330,533 | 1,107,999 | 2023-04-26 19:27:28 | 16,489,942 | 359 | 2013-05-10 19:59:06 | 632,407 | 2017-03-25 16:55:06 | https://stackoverflow.com/q/16489809 | https://stackoverflow.com/a/16489942 | <p>Two simple solutions:</p>
<ol>
<li><p>Execute your code once before the while loop</p>
<pre><code>actions() {
check_if_file_present
# Do other stuff
}
actions #1st execution
while [ current_time <= $cutoff ]; do
actions # Loop execution
done
</code></pre></li>
<li><p>Or:</p>
<pre><code>while : ; do
... | <p>Two simple solutions:</p> <ol> <li><p>Execute your code once before the while loop</p> <pre><code>actions() { check_if_file_present # Do other stuff } actions #1st execution while [ current_time <= $cutoff ]; do actions # Loop execution done </code></pre></li> <li><p>Or:</p> <pre><code>while : ; do ... | 387, 2314, 46955 | bash, do-while, loops | <h1>Emulating a do-while loop in Bash</h1>
<p>What is the best way to emulate a do-while loop in Bash?</p>
<p>I could check for the condition before entering the <code>while</code> loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way?</p>
<p>Pseudo code of ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 556 | bash | # Emulating a do-while loop in Bash
What is the best way to emulate a do-while loop in Bash?
I could check for the condition before entering the `while` loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way?
Pseudo code of my script:
```
while [ current_ti... | Two simple solutions:
1. Execute your code once before the while loop
```
actions() {
check_if_file_present
# Do other stuff
}
actions #1st execution
while [ current_time <= $cutoff ]; do
actions # Loop execution
done
```
2. Or:
```
while : ; do
actions
[[ ... |
34156938 | OpenSSL hangs during PKCS12 export with "Loading 'screen' into random state" | 128 | 2015-12-08 13:17:34 | <p>I am generating a self-signed SSL certificate with OpenSSL (not makecert), for use in IIS.</p>
<pre><code>openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj '//CN=myhost'
</code></pre>
<p>(The double slash is correct. The command above does not work without that.)</p>
<pre><co... | 62,157 | 121,968 | 2022-01-20 07:05:08 | 38,202,633 | 359 | 2016-07-05 11:45:24 | 3,149,528 | 2019-07-31 15:52:25 | https://stackoverflow.com/q/34156938 | https://stackoverflow.com/a/38202633 | <p>Please try to add <a href="https://github.com/rprichard/winpty" rel="noreferrer"><code>winpty</code></a> before oppenssl: </p>
<pre><code>winpty openssl ...
</code></pre>
<p>or you can run a new bash wrapped by winpty:</p>
<pre><code>winpty bash
</code></pre>
<p>In the windows console, there is some problem wit... | <p>Please try to add <a href="https://github.com/rprichard/winpty" rel="noreferrer"><code>winpty</code></a> before oppenssl: </p> <pre><code>winpty openssl ... </code></pre> <p>or you can run a new bash wrapped by winpty:</p> <pre><code>winpty bash </code></pre> <p>In the windows console, there is some problem wit... | 64, 641, 1999, 61874 | git-bash, openssl, ssl, windows | <h1>OpenSSL hangs during PKCS12 export with "Loading 'screen' into random state"</h1>
<p>I am generating a self-signed SSL certificate with OpenSSL (not makecert), for use in IIS.</p>
<pre><code>openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj '//CN=myhost'
</code></pre>
<p>(The... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 557 | bash | # OpenSSL hangs during PKCS12 export with "Loading 'screen' into random state"
I am generating a self-signed SSL certificate with OpenSSL (not makecert), for use in IIS.
```
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj '//CN=myhost'
```
(The double slash is correct. The com... | Please try to add [`winpty`](https://github.com/rprichard/winpty) before oppenssl:
```
winpty openssl ...
```
or you can run a new bash wrapped by winpty:
```
winpty bash
```
In the windows console, there is some problem with terminal input/output so winpty can help if some software requires `unix` terminal behavio... |
10286164 | Function return value in PowerShell | 269 | 2012-04-23 18:28:39 | <p>I have developed a PowerShell function that performs a number of actions involving provisioning <a href="http://en.wikipedia.org/wiki/Microsoft_SharePoint" rel="noreferrer">SharePoint</a> Team sites. Ultimately, I want the function to return the URL of the provisioned site as a String so at the end of my function I ... | 776,477 | 284,560 | 2021-08-27 17:16:49 | 10,288,256 | 358 | 2012-04-23 21:01:56 | 243 | 2012-04-23 21:01:56 | https://stackoverflow.com/q/10286164 | https://stackoverflow.com/a/10288256 | <p>PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:</p>
<ul>
<li>All output is captured, and returned</li>
<li>The return keyword really just indicates a logical exit point</li>
</ul>
<p>Thus, the fo... | <p>PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:</p> <ul> <li>All output is captured, and returned</li> <li>The return keyword really just indicates a logical exit point</li> </ul> <p>Thus, the fo... | 526 | powershell | <h1>Function return value in PowerShell</h1>
<p>I have developed a PowerShell function that performs a number of actions involving provisioning <a href="http://en.wikipedia.org/wiki/Microsoft_SharePoint" rel="noreferrer">SharePoint</a> Team sites. Ultimately, I want the function to return the URL of the provisioned sit... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 558 | bash | # Function return value in PowerShell
I have developed a PowerShell function that performs a number of actions involving provisioning [SharePoint](http://en.wikipedia.org/wiki/Microsoft_SharePoint) Team sites. Ultimately, I want the function to return the URL of the provisioned site as a String so at the end of my fun... | PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
- All output is captured, and returned
- The return keyword really just indicates a logical exit point
Thus, the following two script blocks will do e... |
13702425 | source command not found in sh shell | 335 | 2012-12-04 12:01:43 | <p>I have a script that uses <code>sh</code> shell. I get an error in the line that uses the <code>source</code> command. It seems <code>source</code> is not included in my <code>sh</code> shell. </p>
<p>If I explicitly try to run <code>source</code> from shell I get:</p>
<pre><code>sh: 1: source: not found
</code></... | 587,607 | 298,209 | 2025-03-02 14:13:10 | 13,702,876 | 356 | 2012-12-04 12:26:30 | 1,030,675 | 2018-09-02 20:51:46 | https://stackoverflow.com/q/13702425 | https://stackoverflow.com/a/13702876 | <p><code>/bin/sh</code> is usually some other shell trying to mimic The Shell. Many distributions use <code>/bin/bash</code> for <code>sh</code>, it supports <code>source</code>. On Ubuntu, though, <code>/bin/dash</code> is used which does not support <code>source</code>. Most shells use <code>.</code> instead of <code... | <p><code>/bin/sh</code> is usually some other shell trying to mimic The Shell. Many distributions use <code>/bin/bash</code> for <code>sh</code>, it supports <code>source</code>. On Ubuntu, though, <code>/bin/dash</code> is used which does not support <code>source</code>. Most shells use <code>.</code> instead of <code... | 387, 390, 10327 | bash, sh, shell | <h1>source command not found in sh shell</h1>
<p>I have a script that uses <code>sh</code> shell. I get an error in the line that uses the <code>source</code> command. It seems <code>source</code> is not included in my <code>sh</code> shell. </p>
<p>If I explicitly try to run <code>source</code> from shell I get:</p>
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 559 | bash | # source command not found in sh shell
I have a script that uses `sh` shell. I get an error in the line that uses the `source` command. It seems `source` is not included in my `sh` shell.
If I explicitly try to run `source` from shell I get:
```
sh: 1: source: not found
```
Should I somehow install "source"? Do I h... | `/bin/sh` is usually some other shell trying to mimic The Shell. Many distributions use `/bin/bash` for `sh`, it supports `source`. On Ubuntu, though, `/bin/dash` is used which does not support `source`. Most shells use `.` instead of `source`. If you cannot edit the script, try to change the shell which runs it. |
39800481 | Display all environment variables from a running PowerShell script | 246 | 2016-09-30 21:41:31 | <p>I need to display all configured environment variables in a PowerShell script at runtime. Normally when displaying environment variables I can just use one of the following at the shell (among other techniques, but these are simple):</p>
<pre><code>gci env:*
ls Env:
</code></pre>
<p>However, I have a script being ... | 325,445 | 584,676 | 2024-11-18 19:08:59 | 45,570,821 | 356 | 2017-08-08 14:17:14 | 946,773 | 2018-11-02 07:45:03 | https://stackoverflow.com/q/39800481 | https://stackoverflow.com/a/45570821 | <p>Shorter version: </p>
<pre><code>gci env:* | sort-object name
</code></pre>
<p>This will display both the name and value.</p>
| <p>Shorter version: </p> <pre><code>gci env:* | sort-object name </code></pre> <p>This will display both the name and value.</p> | 526, 9013 | environment-variables, powershell | <h1>Display all environment variables from a running PowerShell script</h1>
<p>I need to display all configured environment variables in a PowerShell script at runtime. Normally when displaying environment variables I can just use one of the following at the shell (among other techniques, but these are simple):</p>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 560 | bash | # Display all environment variables from a running PowerShell script
I need to display all configured environment variables in a PowerShell script at runtime. Normally when displaying environment variables I can just use one of the following at the shell (among other techniques, but these are simple):
```
gci env:*
l... | Shorter version:
```
gci env:* | sort-object name
```
This will display both the name and value. |
16317961 | How to process each output line in a loop? | 207 | 2013-05-01 12:19:36 | <p>I have a number of lines retrieved from a file after running the <a href="http://linux.die.net/man/1/grep" rel="noreferrer">grep</a> command as follows:</p>
<pre><code>var=`grep xyz abc.txt`
</code></pre>
<p>Let’s say I got 10 lines which consists of xyz as a result.</p>
<p>Now I need to process each line I got a... | 258,246 | 1,468,315 | 2020-12-01 05:00:00 | 16,318,005 | 356 | 2013-05-01 12:23:42 | 635,608 | 2015-07-24 04:43:26 | https://stackoverflow.com/q/16317961 | https://stackoverflow.com/a/16318005 | <p>One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.</p>
<p>Something like:</p>
<pre><code>grep xyz abc.txt | while read -r line ; do
echo "Processing $line"
# your code goes here
done
</code></pre>
<p>There are variations on this scheme depen... | <p>One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.</p> <p>Something like:</p> <pre><code>grep xyz abc.txt | while read -r line ; do echo "Processing $line" # your code goes here done </code></pre> <p>There are variations on this scheme depen... | 387, 390, 1271 | bash, grep, shell | <h1>How to process each output line in a loop?</h1>
<p>I have a number of lines retrieved from a file after running the <a href="http://linux.die.net/man/1/grep" rel="noreferrer">grep</a> command as follows:</p>
<pre><code>var=`grep xyz abc.txt`
</code></pre>
<p>Let’s say I got 10 lines which consists of xyz as a res... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 561 | bash | # How to process each output line in a loop?
I have a number of lines retrieved from a file after running the [grep](http://linux.die.net/man/1/grep) command as follows:
```
var=`grep xyz abc.txt`
```
Let’s say I got 10 lines which consists of xyz as a result.
Now I need to process each line I got as a result of th... | One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.
Something like:
```
grep xyz abc.txt | while read -r line ; do
echo "Processing $line"
# your code goes here
done
```
There are variations on this scheme depending on exactly what you're after.... |
9772036 | Pass all variables from one shell script to another? | 272 | 2012-03-19 14:30:23 | <p>Lets say I have a shell / bash script named <code>test.sh</code> with:</p>
<pre><code>#!/bin/bash
TESTVARIABLE=hellohelloheloo
./test2.sh
</code></pre>
<p>My <code>test2.sh</code> looks like this:</p>
<pre><code>#!/bin/bash
echo ${TESTVARIABLE}
</code></pre>
<p>This does not work. I do not want to pass all var... | 360,832 | 533,426 | 2025-04-02 09:06:05 | 9,772,093 | 354 | 2012-03-19 14:34:31 | 1,172,428 | 2012-03-20 13:49:35 | https://stackoverflow.com/q/9772036 | https://stackoverflow.com/a/9772093 | <p>You have basically two options:</p>
<ol>
<li>Make the variable an environment variable (<code>export TESTVARIABLE</code>) before executing the 2nd script.</li>
<li>Source the 2nd script, i.e. <code>. test2.sh</code> and it will run in the same shell. This would let you share more complex variables like arrays easil... | <p>You have basically two options:</p> <ol> <li>Make the variable an environment variable (<code>export TESTVARIABLE</code>) before executing the 2nd script.</li> <li>Source the 2nd script, i.e. <code>. test2.sh</code> and it will run in the same shell. This would let you share more complex variables like arrays easil... | 387, 390 | bash, shell | <h1>Pass all variables from one shell script to another?</h1>
<p>Lets say I have a shell / bash script named <code>test.sh</code> with:</p>
<pre><code>#!/bin/bash
TESTVARIABLE=hellohelloheloo
./test2.sh
</code></pre>
<p>My <code>test2.sh</code> looks like this:</p>
<pre><code>#!/bin/bash
echo ${TESTVARIABLE}
</cod... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 562 | bash | # Pass all variables from one shell script to another?
Lets say I have a shell / bash script named `test.sh` with:
```
#!/bin/bash
TESTVARIABLE=hellohelloheloo
./test2.sh
```
My `test2.sh` looks like this:
```
#!/bin/bash
echo ${TESTVARIABLE}
```
This does not work. I do not want to pass all variables as paramet... | You have basically two options:
1. Make the variable an environment variable (`export TESTVARIABLE`) before executing the 2nd script.
2. Source the 2nd script, i.e. `. test2.sh` and it will run in the same shell. This would let you share more complex variables like arrays easily, but also means that the other script c... |
5013151 | How do I limit the number of results returned from grep? | 264 | 2011-02-16 06:23:23 | <p>I would like to say 10 lines max from grep.</p>
<p>I don't want my computer to work hard. I want it to stop after 10 results found by grep. Is it possible?</p>
| 247,931 | 345,859 | 2024-05-22 10:11:20 | 5,013,198 | 353 | 2011-02-16 06:29:06 | 33,888 | 2019-10-10 20:28:56 | https://stackoverflow.com/q/5013151 | https://stackoverflow.com/a/5013198 | <p>The <code>-m</code> option is probably what you're looking for:</p>
<pre><code>grep -m 10 PATTERN [FILE]
</code></pre>
<p>From <code>man grep</code>:</p>
<pre class="lang-none prettyprint-override"><code>-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is
stand... | <p>The <code>-m</code> option is probably what you're looking for:</p> <pre><code>grep -m 10 PATTERN [FILE] </code></pre> <p>From <code>man grep</code>:</p> <pre class="lang-none prettyprint-override"><code>-m NUM, --max-count=NUM Stop reading a file after NUM matching lines. If the input is stand... | 34, 58, 387, 10327 | bash, linux, sh, unix | <h1>How do I limit the number of results returned from grep?</h1>
<p>I would like to say 10 lines max from grep.</p>
<p>I don't want my computer to work hard. I want it to stop after 10 results found by grep. Is it possible?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 563 | bash | # How do I limit the number of results returned from grep?
I would like to say 10 lines max from grep.
I don't want my computer to work hard. I want it to stop after 10 results found by grep. Is it possible? | The `-m` option is probably what you're looking for:
```
grep -m 10 PATTERN [FILE]
```
From `man grep`:
```
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is
standard input from a regular file, and NUM matching lines are
output, grep ensures that the s... |
8264655 | How to make PowerShell tab completion work like Bash | 229 | 2011-11-25 03:46:43 | <p>Let's say I have the following files in my current directory:</p>
<pre><code>buildBar.bat
buildFoo.bat
buildHouse.bat
</code></pre>
<p>And I type the following at my command prompt, <code>./bu</code> and then <kbd>TAB</kbd>.</p>
<ul>
<li><p>In Bash, it gets expanded to <code>./build</code></p></li>
<li><p>In Powe... | 95,930 | 270,348 | 2024-07-05 16:34:13 | 37,715,242 | 353 | 2016-06-09 00:51:41 | 41,071 | 2023-02-14 09:04:26 | https://stackoverflow.com/q/8264655 | https://stackoverflow.com/a/37715242 | <p>New versions of PowerShell include PSReadline, which can be used to do this:</p>
<pre><code>Set-PSReadlineKeyHandler -Key Tab -Function Complete
</code></pre>
<p>or, to make it even more like bash where you can use arrow-keys to navigate available options:</p>
<pre><code>Set-PSReadlineKeyHandler -Key Tab -Function M... | <p>New versions of PowerShell include PSReadline, which can be used to do this:</p> <pre><code>Set-PSReadlineKeyHandler -Key Tab -Function Complete </code></pre> <p>or, to make it even more like bash where you can use arrow-keys to navigate available options:</p> <pre><code>Set-PSReadlineKeyHandler -Key Tab -Function M... | 526, 2631, 12891 | cmd, powershell, tab-completion | <h1>How to make PowerShell tab completion work like Bash</h1>
<p>Let's say I have the following files in my current directory:</p>
<pre><code>buildBar.bat
buildFoo.bat
buildHouse.bat
</code></pre>
<p>And I type the following at my command prompt, <code>./bu</code> and then <kbd>TAB</kbd>.</p>
<ul>
<li><p>In Bash, it... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 564 | bash | # How to make PowerShell tab completion work like Bash
Let's say I have the following files in my current directory:
```
buildBar.bat
buildFoo.bat
buildHouse.bat
```
And I type the following at my command prompt, `./bu` and then `TAB`.
- In Bash, it gets expanded to `./build`
- In PowerShell, it gets expanded to `.... | New versions of PowerShell include PSReadline, which can be used to do this:
```
Set-PSReadlineKeyHandler -Key Tab -Function Complete
```
or, to make it even more like bash where you can use arrow-keys to navigate available options:
```
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
```
To make it permane... |
31879814 | Check if a file exists or not in Windows PowerShell? | 226 | 2015-08-07 14:12:17 | <p>I have this script which compares files in two areas of the disk and copies the latest file over the one with the older modified date. </p>
<pre><code>$filestowatch=get-content C:\H\files-to-watch.txt
$adminFiles=dir C:\H\admin\admin -recurse | ? { $fn=$_.FullName; ($filestowatch | % {$fn.contains($_)}) -contains ... | 679,481 | 975,566 | 2020-07-13 14:25:23 | 31,881,297 | 353 | 2015-08-07 15:21:16 | 712,649 | 2015-08-07 15:21:16 | https://stackoverflow.com/q/31879814 | https://stackoverflow.com/a/31881297 | <p>Just to offer <a href="https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx" rel="noreferrer">the alternative</a> to the <a href="https://technet.microsoft.com/en-us/library/ee177015.aspx" rel="noreferrer"><code>Test-Path</code> cmdlet</a> (since nobody mentioned it):</p>
<pre><code>[System... | <p>Just to offer <a href="https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx" rel="noreferrer">the alternative</a> to the <a href="https://technet.microsoft.com/en-us/library/ee177015.aspx" rel="noreferrer"><code>Test-Path</code> cmdlet</a> (since nobody mentioned it):</p> <pre><code>[System... | 526, 73157 | powershell, powershell-3.0 | <h1>Check if a file exists or not in Windows PowerShell?</h1>
<p>I have this script which compares files in two areas of the disk and copies the latest file over the one with the older modified date. </p>
<pre><code>$filestowatch=get-content C:\H\files-to-watch.txt
$adminFiles=dir C:\H\admin\admin -recurse | ? { $fn=... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 565 | bash | # Check if a file exists or not in Windows PowerShell?
I have this script which compares files in two areas of the disk and copies the latest file over the one with the older modified date.
```
$filestowatch=get-content C:\H\files-to-watch.txt
$adminFiles=dir C:\H\admin\admin -recurse | ? { $fn=$_.FullName; ($filest... | Just to offer [the alternative](https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx) to the [`Test-Path` cmdlet](https://technet.microsoft.com/en-us/library/ee177015.aspx) (since nobody mentioned it):
```
[System.IO.File]::Exists($path)
```
Does (almost) the same thing as
```
Test-Path $pat... |
6834487 | What is the $? (dollar question mark) variable in shell scripting? | 343 | 2011-07-26 18:08:14 | <p>I'm trying to learn shell scripting, and I need to understand someone else's code. What is the <code>$?</code> variable hold? I can't Google search the answer because they block punctuation characters.</p>
| 263,956 | 663,279 | 2020-03-26 17:44:51 | 6,834,572 | 352 | 2011-07-26 18:15:10 | 863,378 | 2017-06-29 10:35:24 | https://stackoverflow.com/q/6834487 | https://stackoverflow.com/a/6834572 | <p><code>$?</code> is used to find the return value of the last executed command.
Try the following in the shell:</p>
<pre><code>ls somefile
echo $?
</code></pre>
<p>If <code>somefile</code> exists (regardless whether it is a file or directory), you will get the return value thrown by the <code>ls</code> command, whi... | <p><code>$?</code> is used to find the return value of the last executed command. Try the following in the shell:</p> <pre><code>ls somefile echo $? </code></pre> <p>If <code>somefile</code> exists (regardless whether it is a file or directory), you will get the return value thrown by the <code>ls</code> command, whi... | 390, 531 | scripting, shell | <h1>What is the $? (dollar question mark) variable in shell scripting?</h1>
<p>I'm trying to learn shell scripting, and I need to understand someone else's code. What is the <code>$?</code> variable hold? I can't Google search the answer because they block punctuation characters.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 566 | bash | # What is the $? (dollar question mark) variable in shell scripting?
I'm trying to learn shell scripting, and I need to understand someone else's code. What is the `$?` variable hold? I can't Google search the answer because they block punctuation characters. | `$?` is used to find the return value of the last executed command.
Try the following in the shell:
```
ls somefile
echo $?
```
If `somefile` exists (regardless whether it is a file or directory), you will get the return value thrown by the `ls` command, which should be `0` (default "success" return value). If it doe... |
1593051 | How to programmatically determine the current checked out Git branch | 332 | 2009-10-20 07:52:58 | <p><strong>In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?</strong></p>
<p>One use of this technique would be automatically labeling a release (like <code>svnversion</code> would do with Subve... | 166,516 | 2,938 | 2018-05-31 16:09:05 | 1,593,487 | 352 | 2009-10-20 09:34:32 | 46,058 | 2013-06-11 14:18:04 | https://stackoverflow.com/q/1593051 | https://stackoverflow.com/a/1593487 | <p>The correct solution is to take a peek at <a href="http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/completion/git-completion.bash;hb=HEAD" rel="noreferrer" title="git.kernel.org - git/git.git/blob - contrib/completion/git-completion.bash">contrib/completions/git-completion.bash</a> does that for bash prompt in... | <p>The correct solution is to take a peek at <a href="http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/completion/git-completion.bash;hb=HEAD" rel="noreferrer" title="git.kernel.org - git/git.git/blob - contrib/completion/git-completion.bash">contrib/completions/git-completion.bash</a> does that for bash prompt in... | 119, 387, 390 | bash, git, shell | <h1>How to programmatically determine the current checked out Git branch</h1>
<p><strong>In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?</strong></p>
<p>One use of this technique would be auto... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 567 | bash | # How to programmatically determine the current checked out Git branch
**In a Unix or GNU scripting environment (e.g. a Linux distro, Cygwin, OSX), what is the best way to determine which Git branch is currently checked out in a working directory?**
One use of this technique would be automatically labeling a release ... | The correct solution is to take a peek at [contrib/completions/git-completion.bash](http://git.kernel.org/?p=git/git.git;a=blob;f=contrib/completion/git-completion.bash;hb=HEAD "git.kernel.org - git/git.git/blob - contrib/completion/git-completion.bash") does that for bash prompt in `__git_ps1`. Removing all extras lik... |
14620290 | Array.Add vs += | 269 | 2013-01-31 07:10:36 | <p>I've found some interesting behaviour in PowerShell Arrays, namely, if I declare an array as:</p>
<pre><code>$array = @()
</code></pre>
<p>And then try to add items to it using the <code>$array.Add("item")</code> method, I receive the following error:</p>
<blockquote>
<p>Exception calling "Add" with "1" argumen... | 617,363 | 624,143 | 2022-05-30 19:23:17 | 14,620,446 | 352 | 2013-01-31 07:19:11 | 702,944 | 2018-02-19 19:28:13 | https://stackoverflow.com/q/14620290 | https://stackoverflow.com/a/14620446 | <p>When using the <code>$array.Add()</code>-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.</p>
<p><code>$array += $element</code> creates a <strong>new</strong> array with the same elements as old one ... | <p>When using the <code>$array.Add()</code>-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.</p> <p><code>$array += $element</code> creates a <strong>new</strong> array with the same elements as old one ... | 114, 526 | arrays, powershell | <h1>Array.Add vs +=</h1>
<p>I've found some interesting behaviour in PowerShell Arrays, namely, if I declare an array as:</p>
<pre><code>$array = @()
</code></pre>
<p>And then try to add items to it using the <code>$array.Add("item")</code> method, I receive the following error:</p>
<blockquote>
<p>Exception calli... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 568 | bash | # Array.Add vs +=
I've found some interesting behaviour in PowerShell Arrays, namely, if I declare an array as:
```
$array = @()
```
And then try to add items to it using the `$array.Add("item")` method, I receive the following error:
> Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."... | When using the `$array.Add()`-method, you're trying to add the element into the existing array. An array is a collection of fixed size, so you will receive an error because it can't be extended.
`$array += $element` creates a **new** array with the same elements as old one + the new item, and this new larger array rep... |
21233290 | mongorestore error: Don't know what to do with the dump file | 143 | 2014-01-20 11:50:03 | <p>I have mongo DB installed in the following path <code>c:\mongodb\bin</code>. I have configured my environment variable PATH in advanced settings.I also have mongod running .When I run the following command <strong>mongorestore dump</strong> from the following path <code>c:\hw1-1\dump</code> (This contains the BSON f... | 114,314 | 1,950,969 | 2017-10-20 17:05:05 | 29,640,961 | 352 | 2015-04-15 03:08:57 | 3,612,327 | 2015-04-15 03:08:57 | https://stackoverflow.com/q/21233290 | https://stackoverflow.com/a/29640961 | <p>in mongodb 3.0 or above, we should specify the database name to restore</p>
<pre><code>mongorestore -d [your_db_name] [your_dump_dir]
</code></pre>
| <p>in mongodb 3.0 or above, we should specify the database name to restore</p> <pre><code>mongorestore -d [your_db_name] [your_dump_dir] </code></pre> | 30073, 69521 | mongodb, mongo-shell | <h1>mongorestore error: Don't know what to do with the dump file</h1>
<p>I have mongo DB installed in the following path <code>c:\mongodb\bin</code>. I have configured my environment variable PATH in advanced settings.I also have mongod running .When I run the following command <strong>mongorestore dump</strong> from t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 569 | bash | # mongorestore error: Don't know what to do with the dump file
I have mongo DB installed in the following path `c:\mongodb\bin`. I have configured my environment variable PATH in advanced settings.I also have mongod running .When I run the following command **mongorestore dump** from the following path `c:\hw1-1\dump`... | in mongodb 3.0 or above, we should specify the database name to restore
```
mongorestore -d [your_db_name] [your_dump_dir]
``` |
46058546 | Error: EACCES: permission denied, access '/usr/lib/node_modules' | 109 | 2017-09-05 15:24:59 | <p>I am trying install typescript with command <code>npm install -g typescript</code>, and it returns this error:</p>
<pre><code> npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! at Error (native)
npm ERR! { Error: EACCES: permission denied, access '/usr/lib/node_module... | 152,960 | 7,389,315 | 2025-01-03 18:26:13 | 49,122,289 | 352 | 2018-03-06 01:56:08 | 8,990,184 | 2023-07-25 21:08:43 | https://stackoverflow.com/q/46058546 | https://stackoverflow.com/a/49122289 | <p><strong>It's not recommended to use sudo with npm install, follow the steps from npmjs official docs instead</strong>.</p>
<p>Make a directory for global installations:</p>
<pre><code>mkdir ~/.npm-global
</code></pre>
<p>Configure npm to use the new directory path:</p>
<pre><code>npm config set prefix '~/.npm-global... | <p><strong>It's not recommended to use sudo with npm install, follow the steps from npmjs official docs instead</strong>.</p> <p>Make a directory for global installations:</p> <pre><code>mkdir ~/.npm-global </code></pre> <p>Configure npm to use the new directory path:</p> <pre><code>npm config set prefix '~/.npm-global... | 390, 61387 | npm, shell | <h1>Error: EACCES: permission denied, access '/usr/lib/node_modules'</h1>
<p>I am trying install typescript with command <code>npm install -g typescript</code>, and it returns this error:</p>
<pre><code> npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! at Error (native)
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 570 | bash | # Error: EACCES: permission denied, access '/usr/lib/node_modules'
I am trying install typescript with command `npm install -g typescript`, and it returns this error:
```
npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! at Error (native)
npm ERR! { Error: EACCES: per... | **It's not recommended to use sudo with npm install, follow the steps from npmjs official docs instead**.
Make a directory for global installations:
```
mkdir ~/.npm-global
```
Configure npm to use the new directory path:
```
npm config set prefix '~/.npm-global'
```
Open or create a ~/.profile file and add this l... |
1469849 | How to split one string into multiple strings separated by at least one space in bash shell? | 365 | 2009-09-24 05:07:37 | <p>I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?</p>
<p>The string is passed as an argument. E.g. <code>${2} == "cat cat file"</code>. How can I loop through it?</p>
<p>Also, how can I check if a string con... | 673,382 | 115,622 | 2024-01-23 05:01:18 | 1,469,863 | 350 | 2009-09-24 05:13:46 | 168,657 | 2009-09-24 05:13:46 | https://stackoverflow.com/q/1469849 | https://stackoverflow.com/a/1469863 | <p>Did you try just passing the string variable to a <code>for</code> loop? Bash, for one, will split on whitespace automatically.</p>
<pre><code>sentence="This is a sentence."
for word in $sentence
do
echo $word
done
</code></pre>
<p> </p>
<pre><code>This
is
a
sentence.
</code></pre>
| <p>Did you try just passing the string variable to a <code>for</code> loop? Bash, for one, will split on whitespace automatically.</p> <pre><code>sentence="This is a sentence." for word in $sentence do echo $word done </code></pre> <p> </p> <pre><code>This is a sentence. </code></pre> | 139, 387, 390, 2193 | bash, shell, split, string | <h1>How to split one string into multiple strings separated by at least one space in bash shell?</h1>
<p>I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?</p>
<p>The string is passed as an argument. E.g. <code>${... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 571 | bash | # How to split one string into multiple strings separated by at least one space in bash shell?
I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?
The string is passed as an argument. E.g. `${2} == "cat cat file"`... | Did you try just passing the string variable to a `for` loop? Bash, for one, will split on whitespace automatically.
```
sentence="This is a sentence."
for word in $sentence
do
echo $word
done
```
```
This
is
a
sentence.
``` |
4542732 | How do I negate a test with regular expressions in a bash script? | 300 | 2010-12-27 23:53:47 | <p>Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with Regular Expressions. For example, I would like to conditionally add a path to the PATH variable, if the path is not already there, as in:</p>
<pre><code>TEMP=/mnt/silo/bin
if [[ ${PATH} =~ ${TEMP} ]] ; then PATH=$P... | 246,197 | 214,961 | 2022-06-28 19:19:26 | 4,542,761 | 350 | 2010-12-28 00:00:12 | 207,248 | 2010-12-28 00:00:12 | https://stackoverflow.com/q/4542732 | https://stackoverflow.com/a/4542761 | <p>You had it right, just put a space between the <code>!</code> and the <code>[[</code> like <code>if ! [[</code></p>
| <p>You had it right, just put a space between the <code>!</code> and the <code>[[</code> like <code>if ! [[</code></p> | 18, 387, 19628, 39271 | bash, conditional-statements, negate, regex | <h1>How do I negate a test with regular expressions in a bash script?</h1>
<p>Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with Regular Expressions. For example, I would like to conditionally add a path to the PATH variable, if the path is not already there, as in:</p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 572 | bash | # How do I negate a test with regular expressions in a bash script?
Using GNU bash (version 4.0.35(1)-release (x86_64-suse-linux-gnu), I would like to negate a test with Regular Expressions. For example, I would like to conditionally add a path to the PATH variable, if the path is not already there, as in:
```
TEMP=/... | You had it right, just put a space between the `!` and the `[[` like `if ! [[` |
21612980 | Why is #!/usr/bin/env bash superior to #!/bin/bash? | 341 | 2014-02-06 20:09:29 | <p>I've seen in a number of places, including recommendations on this site (<a href="https://stackoverflow.com/questions/10376206/preferred-bash-shebang">What is the preferred Bash shebang?</a>), to use <code>#!/usr/bin/env bash</code> in preference to <code>#!/bin/bash</code>. I've even seen one enterprising individua... | 140,890 | 2,022,196 | 2024-05-01 10:54:25 | 21,613,044 | 349 | 2014-02-06 20:12:26 | 722,332 | 2014-02-06 20:12:26 | https://stackoverflow.com/q/21612980 | https://stackoverflow.com/a/21613044 | <p><code>#!/usr/bin/env</code> searches <code>PATH</code> for <code>bash</code>, and <code>bash</code> is not always in <code>/bin</code>, particularly on non-Linux systems. For example, on my OpenBSD system, it's in <code>/usr/local/bin</code>, since it was installed as an optional package.</p>
<p>If you are absolut... | <p><code>#!/usr/bin/env</code> searches <code>PATH</code> for <code>bash</code>, and <code>bash</code> is not always in <code>/bin</code>, particularly on non-Linux systems. For example, on my OpenBSD system, it's in <code>/usr/local/bin</code>, since it was installed as an optional package.</p> <p>If you are absolut... | 387, 8780 | bash, shebang | <h1>Why is #!/usr/bin/env bash superior to #!/bin/bash?</h1>
<p>I've seen in a number of places, including recommendations on this site (<a href="https://stackoverflow.com/questions/10376206/preferred-bash-shebang">What is the preferred Bash shebang?</a>), to use <code>#!/usr/bin/env bash</code> in preference to <code>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 573 | bash | # Why is #!/usr/bin/env bash superior to #!/bin/bash?
I've seen in a number of places, including recommendations on this site ([What is the preferred Bash shebang?](https://stackoverflow.com/questions/10376206/preferred-bash-shebang)), to use `#!/usr/bin/env bash` in preference to `#!/bin/bash`. I've even seen one ent... | `#!/usr/bin/env` searches `PATH` for `bash`, and `bash` is not always in `/bin`, particularly on non-Linux systems. For example, on my OpenBSD system, it's in `/usr/local/bin`, since it was installed as an optional package.
If you are absolutely sure `bash` is in `/bin` and will always be, there's no harm in putting i... |
18877580 | PowerShell and the -contains operator | 258 | 2013-09-18 16:31:11 | <p>Consider the following snippet:</p>
<pre><code>"12-18" -Contains "-"
</code></pre>
<p>You’d think this evaluates to <code>true</code>, but it doesn't. This will evaluate to <code>false</code> instead. I’m not sure why this happens, but it does.</p>
<p>To avoid this, you can use this instead:</p>
... | 579,484 | 770,270 | 2021-06-09 09:04:56 | 18,877,724 | 348 | 2013-09-18 16:38:32 | 419 | 2019-06-19 14:21:56 | https://stackoverflow.com/q/18877580 | https://stackoverflow.com/a/18877724 | <p>The <code>-Contains</code> operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.</p>
<p>From the documentation you linked to:</p>
<blockquote>
<p>-Contains
Description: Containment operator. <strong>Tells whether a collection of reference values... | <p>The <code>-Contains</code> operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.</p> <p>From the documentation you linked to:</p> <blockquote> <p>-Contains Description: Containment operator. <strong>Tells whether a collection of reference values... | 526, 7992, 17150 | operators, powershell, string-matching | <h1>PowerShell and the -contains operator</h1>
<p>Consider the following snippet:</p>
<pre><code>"12-18" -Contains "-"
</code></pre>
<p>You’d think this evaluates to <code>true</code>, but it doesn't. This will evaluate to <code>false</code> instead. I’m not sure why this happens, but it does.</p>
<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 574 | bash | # PowerShell and the -contains operator
Consider the following snippet:
```
"12-18" -Contains "-"
```
You’d think this evaluates to `true`, but it doesn't. This will evaluate to `false` instead. I’m not sure why this happens, but it does.
To avoid this, you can use this instead:
```
"12-18".Contains("-")
```
Now ... | The `-Contains` operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.
From the documentation you linked to:
> -Contains
> Description: Containment operator. **Tells whether a collection of reference values includes a single test value.**
In the exampl... |
16203088 | Bash if statement with multiple conditions throws an error | 232 | 2013-04-24 22:09:46 | <p>I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:</p>
<pre><code>my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_e... | 913,242 | 1,810,609 | 2025-10-21 00:47:06 | 16,203,126 | 348 | 2013-04-24 22:12:40 | 646,979 | 2016-02-10 18:14:28 | https://stackoverflow.com/q/16203088 | https://stackoverflow.com/a/16203126 | <p>Use <code>-a</code> (for and) and <code>-o</code> (for or) operations.</p>
<p><a href="http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html">tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html</a></p>
<p>Update</p>
<p>Actually you could still use <code>&&</code> and <code>||</code> with the <c... | <p>Use <code>-a</code> (for and) and <code>-o</code> (for or) operations.</p> <p><a href="http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html">tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html</a></p> <p>Update</p> <p>Actually you could still use <code>&&</code> and <code>||</code> with the <c... | 387, 1079, 2773 | bash, flags, if-statement | <h1>Bash if statement with multiple conditions throws an error</h1>
<p>I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:</p>
<pre><code>my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 575 | bash | # Bash if statement with multiple conditions throws an error
I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
```
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ]... | Use `-a` (for and) and `-o` (for or) operations.
[tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html)
Update
Actually you could still use `&&` and `||` with the `-eq` operation. So your script would be like this:
```
my_error_flag=1
my_error_flag_o=... |
48470049 | Build a JSON string with Bash variables | 199 | 2018-01-26 21:48:24 | <p>I need to read these bash variables into my JSON string and I am not familiar with bash. any help is appreciated. </p>
<pre><code>#!/bin/sh
BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar
JSON_STRING='{"bucketname":"$BUCKET_NAME"","objectname":"$OBJECT_NA... | 262,237 | 2,663,805 | 2024-06-05 06:39:33 | 48,470,227 | 348 | 2018-01-26 22:03:33 | 1,126,841 | 2018-01-26 22:03:33 | https://stackoverflow.com/q/48470049 | https://stackoverflow.com/a/48470227 | <p>You are better off using a program like <code>jq</code> to generate the JSON, if you don't know ahead of time if the contents of the variables are properly escaped for inclusion in JSON. Otherwise, you will just end up with invalid JSON for your trouble.</p>
<pre><code>BUCKET_NAME=testbucket
OBJECT_NAME=testworkflo... | <p>You are better off using a program like <code>jq</code> to generate the JSON, if you don't know ahead of time if the contents of the variables are properly escaped for inclusion in JSON. Otherwise, you will just end up with invalid JSON for your trouble.</p> <pre><code>BUCKET_NAME=testbucket OBJECT_NAME=testworkflo... | 387, 1508, 10804 | bash, json, quoting | <h1>Build a JSON string with Bash variables</h1>
<p>I need to read these bash variables into my JSON string and I am not familiar with bash. any help is appreciated. </p>
<pre><code>#!/bin/sh
BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar
JSON_STRING='{"buc... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 576 | bash | # Build a JSON string with Bash variables
I need to read these bash variables into my JSON string and I am not familiar with bash. any help is appreciated.
```
#!/bin/sh
BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar
JSON_STRING='{"bucketname":"$BUCKET_NAM... | You are better off using a program like `jq` to generate the JSON, if you don't know ahead of time if the contents of the variables are properly escaped for inclusion in JSON. Otherwise, you will just end up with invalid JSON for your trouble.
```
BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATI... |
14155596 | How to substitute shell variables in complex text files | 175 | 2013-01-04 10:43:48 | <p>I have several text files in which I have introduced shell variables ($VAR1 or $VAR2 for instance).</p>
<p>I would like to take those files (one by one) and save them in new files where all variables would have been replaced.</p>
<p>To do this, I used the following shell script (found on StackOverflow):</p>
<pre>... | 237,697 | 674,552 | 2023-09-04 15:12:16 | 14,157,575 | 348 | 2013-01-04 12:54:10 | 27,727 | 2020-05-20 20:14:36 | https://stackoverflow.com/q/14155596 | https://stackoverflow.com/a/14157575 | <p>Looking, it turns out on my system there is an <a href="https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html" rel="noreferrer"><code>envsubst</code></a> command which is part of the gettext-base package. </p>
<p>So, this makes it easy: </p>
<pre><code>envsubst < "source.txt" > "dest... | <p>Looking, it turns out on my system there is an <a href="https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html" rel="noreferrer"><code>envsubst</code></a> command which is part of the gettext-base package. </p> <p>So, this makes it easy: </p> <pre><code>envsubst < "source.txt" > "dest... | 58, 390, 25968 | linux, shell, string-interpolation | <h1>How to substitute shell variables in complex text files</h1>
<p>I have several text files in which I have introduced shell variables ($VAR1 or $VAR2 for instance).</p>
<p>I would like to take those files (one by one) and save them in new files where all variables would have been replaced.</p>
<p>To do this, I use... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 577 | bash | # How to substitute shell variables in complex text files
I have several text files in which I have introduced shell variables ($VAR1 or $VAR2 for instance).
I would like to take those files (one by one) and save them in new files where all variables would have been replaced.
To do this, I used the following shell s... | Looking, it turns out on my system there is an [`envsubst`](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) command which is part of the gettext-base package.
So, this makes it easy:
```
envsubst < "source.txt" > "destination.txt"
```
Note if you want to use the same file for both, yo... |
2711001 | How to apply shell command to each line of a command output? | 328 | 2010-04-26 03:31:30 | <p>Suppose I have some output from a command (such as <code>ls -1</code>):</p>
<pre><code>a
b
c
d
e
...
</code></pre>
<p>I want to apply a command (say <code>echo</code>) to each one, in turn. E.g.</p>
<pre><code>echo a
echo b
echo c
echo d
echo e
...
</code></pre>
<p>What's the easiest way to do that in bash?</p>
| 274,498 | 112,950 | 2025-10-28 13:32:54 | 2,711,011 | 347 | 2010-04-26 03:34:48 | 309,308 | 2025-10-28 13:30:43 | https://stackoverflow.com/q/2711001 | https://stackoverflow.com/a/2711011 | <p>It's probably easiest to use <code>xargs</code>. In your case:</p>
<pre><code>ls -1 | xargs -L1 echo
</code></pre>
<p>The <code>-L</code> flag ensures the input is read properly. From the man page of <code>xargs</code>:</p>
<pre class="lang-none prettyprint-override"><code>-L number
Call utility for every number... | <p>It's probably easiest to use <code>xargs</code>. In your case:</p> <pre><code>ls -1 | xargs -L1 echo </code></pre> <p>The <code>-L</code> flag ensures the input is read properly. From the man page of <code>xargs</code>:</p> <pre class="lang-none prettyprint-override"><code>-L number Call utility for every number... | 387 | bash | <h1>How to apply shell command to each line of a command output?</h1>
<p>Suppose I have some output from a command (such as <code>ls -1</code>):</p>
<pre><code>a
b
c
d
e
...
</code></pre>
<p>I want to apply a command (say <code>echo</code>) to each one, in turn. E.g.</p>
<pre><code>echo a
echo b
echo c
echo d
echo e... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 578 | bash | # How to apply shell command to each line of a command output?
Suppose I have some output from a command (such as `ls -1`):
```
a
b
c
d
e
...
```
I want to apply a command (say `echo`) to each one, in turn. E.g.
```
echo a
echo b
echo c
echo d
echo e
...
```
What's the easiest way to do that in bash? | It's probably easiest to use `xargs`. In your case:
```
ls -1 | xargs -L1 echo
```
The `-L` flag ensures the input is read properly. From the man page of `xargs`:
```
-L number
Call utility for every number non-empty lines read.
A line ending with a space continues to the next non-empty line. [...]
``` |
56974927 | "Permission Denied" trying to run Python on Windows 10 | 218 | 2019-07-10 16:23:38 | <p>Seems as though an update on Windows 10 overnight broke Python. Just trying to run <code>python --version</code> returned a "Permission Denied" error. None of the three updates; KB4507453, KB4506991, or KB4509096 look like they'd be the culprit but the timing of the issue is suspicious. Rather than messing with rol... | 353,955 | 83,101 | 2023-09-19 17:52:35 | 57,168,165 | 346 | 2019-07-23 16:03:54 | 83,101 | 2020-06-06 15:27:11 | https://stackoverflow.com/q/56974927 | https://stackoverflow.com/a/57168165 | <p>As far as I can tell, this was caused by a conflict with the version of Python 3.7 that was recently added into the Windows Store. It looks like this added two "stubs" called python.exe and python3.exe into the <code>%USERPROFILE%\AppData\Local\Microsoft\WindowsApps</code> folder, and in my case, this was inserted <... | <p>As far as I can tell, this was caused by a conflict with the version of Python 3.7 that was recently added into the Windows Store. It looks like this added two "stubs" called python.exe and python3.exe into the <code>%USERPROFILE%\AppData\Local\Microsoft\WindowsApps</code> folder, and in my case, this was inserted <... | 16, 2120, 60010, 61874, 84138 | file-permissions, git-bash, python, python-3.x, windows-store-apps | <h1>"Permission Denied" trying to run Python on Windows 10</h1>
<p>Seems as though an update on Windows 10 overnight broke Python. Just trying to run <code>python --version</code> returned a "Permission Denied" error. None of the three updates; KB4507453, KB4506991, or KB4509096 look like they'd be the culprit but the... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 579 | bash | # "Permission Denied" trying to run Python on Windows 10
Seems as though an update on Windows 10 overnight broke Python. Just trying to run `python --version` returned a "Permission Denied" error. None of the three updates; KB4507453, KB4506991, or KB4509096 look like they'd be the culprit but the timing of the issue ... | As far as I can tell, this was caused by a conflict with the version of Python 3.7 that was recently added into the Windows Store. It looks like this added two "stubs" called python.exe and python3.exe into the `%USERPROFILE%\AppData\Local\Microsoft\WindowsApps` folder, and in my case, this was inserted **before** my e... |
6709349 | How to clear all history in linux/ubuntu terminal or bash permanently? | 171 | 2011-07-15 15:21:30 | <p>When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command.</p>
<p>How can I delete that history permanently?</p>
| 211,153 | 698,641 | 2021-08-12 08:03:30 | 6,709,403 | 346 | 2011-07-15 15:24:51 | 682,698 | 2016-08-11 19:55:07 | https://stackoverflow.com/q/6709349 | https://stackoverflow.com/a/6709403 | <p>You can clear your bash history like this:</p>
<p><code>history -cw</code></p>
| <p>You can clear your bash history like this:</p> <p><code>history -cw</code></p> | 58, 387, 391, 549 | bash, linux, terminal, ubuntu | <h1>How to clear all history in linux/ubuntu terminal or bash permanently?</h1>
<p>When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command.</p>
<p>How can I delete that history permanently?</p>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 580 | bash | # How to clear all history in linux/ubuntu terminal or bash permanently?
When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command.
How can I delete that history permanently? | You can clear your bash history like this:
`history -cw` |
6016436 | In PowerShell, how do I define a function in a file and call it from the PowerShell commandline? | 342 | 2011-05-16 11:04:50 | <p>I have a .ps1 file in which I want to define custom functions.</p>
<p>Imagine the file is called MyFunctions.ps1, and the content is as follows:</p>
<pre><code>Write-Host "Installing functions"
function A1
{
Write-Host "A1 is running!"
}
Write-Host "Done"
</code></pre>
<p>To run this script and theoretically ... | 372,326 | 22,702 | 2024-10-17 22:20:08 | 6,016,630 | 345 | 2011-05-16 11:22:42 | 487,296 | 2021-03-24 17:45:35 | https://stackoverflow.com/q/6016436 | https://stackoverflow.com/a/6016630 | <p>Try this on the PowerShell command line:</p>
<pre><code>. .\MyFunctions.ps1
A1
</code></pre>
<p>The dot operator is used for script include, aka "dot-sourcing" (or <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#using-dot-source-... | <p>Try this on the PowerShell command line:</p> <pre><code>. .\MyFunctions.ps1 A1 </code></pre> <p>The dot operator is used for script include, aka "dot-sourcing" (or <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#using-dot-source-... | 526, 151644 | dot-source, powershell | <h1>In PowerShell, how do I define a function in a file and call it from the PowerShell commandline?</h1>
<p>I have a .ps1 file in which I want to define custom functions.</p>
<p>Imagine the file is called MyFunctions.ps1, and the content is as follows:</p>
<pre><code>Write-Host "Installing functions"
function A1
{
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 581 | bash | # In PowerShell, how do I define a function in a file and call it from the PowerShell commandline?
I have a .ps1 file in which I want to define custom functions.
Imagine the file is called MyFunctions.ps1, and the content is as follows:
```
Write-Host "Installing functions"
function A1
{
Write-Host "A1 is runnin... | Try this on the PowerShell command line:
```
. .\MyFunctions.ps1
A1
```
The dot operator is used for script include, aka "dot-sourcing" (or ["dot source notation"](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#using-dot-source-notation-with-scope)... |
22697688 | How to cat <<EOF >> a file containing code? | 230 | 2014-03-27 19:45:01 | <p>I want to print code into a file using <code>cat <<EOF >></code>:</p>
<pre><code>cat <<EOF >> brightup.sh
!/bin/bash
curr=`cat /sys/class/backlight/intel_backlight/actual_brightness`
if [ $curr -lt 4477 ]; then
curr=$((curr+406));
echo $curr > /sys/class/backlight/intel_backlight/b... | 422,927 | 1,743,822 | 2025-06-15 12:13:47 | 22,698,106 | 345 | 2014-03-27 20:05:51 | 874,188 | 2023-10-20 13:14:44 | https://stackoverflow.com/q/22697688 | https://stackoverflow.com/a/22698106 | <p>You only need a minimal change; single-quote the here-document delimiter after <code><<</code>.</p>
<pre><code>cat <<'EOF' >> brightup.sh
</code></pre>
<p>or equivalently backslash-escape it:</p>
<pre><code>cat <<\EOF >>brightup.sh
</code></pre>
<p>Without quoting, the here document wil... | <p>You only need a minimal change; single-quote the here-document delimiter after <code><<</code>.</p> <pre><code>cat <<'EOF' >> brightup.sh </code></pre> <p>or equivalently backslash-escape it:</p> <pre><code>cat <<\EOF >>brightup.sh </code></pre> <p>Without quoting, the here document wil... | 34, 58, 7449, 10327 | heredoc, linux, sh, unix | <h1>How to cat <<EOF >> a file containing code?</h1>
<p>I want to print code into a file using <code>cat <<EOF >></code>:</p>
<pre><code>cat <<EOF >> brightup.sh
!/bin/bash
curr=`cat /sys/class/backlight/intel_backlight/actual_brightness`
if [ $curr -lt 4477 ]; then
curr=$((curr+406));
ec... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 582 | bash | # How to cat <> a file containing code?
I want to print code into a file using `cat <<EOF >>`:
```
cat <<EOF >> brightup.sh
!/bin/bash
curr=`cat /sys/class/backlight/intel_backlight/actual_brightness`
if [ $curr -lt 4477 ]; then
curr=$((curr+406));
echo $curr > /sys/class/backlight/intel_backlight/brightness;
... | You only need a minimal change; single-quote the here-document delimiter after `<<`.
```
cat <<'EOF' >> brightup.sh
```
or equivalently backslash-escape it:
```
cat <<\EOF >>brightup.sh
```
Without quoting, the here document will undergo variable substitution, backticks will be evaluated, etc, like you discovered.
... |
7522712 | How can I check if a command exists in a shell script? | 229 | 2011-09-22 23:34:25 | <p>I am writing my first shell script. In my script I would like to check if a certain command exists, and if not, install the executable. How would I check if this command exists?</p>
<pre><code>if # Check that foobar command doesnt exist
then
# Now install foobar
fi
</code></pre>
| 203,119 | 48,523 | 2020-03-30 11:15:29 | 7,522,866 | 345 | 2011-09-22 23:59:45 | 110,225 | 2018-03-25 21:59:44 | https://stackoverflow.com/q/7522712 | https://stackoverflow.com/a/7522866 | <p>In general, that depends on your shell, but if you use bash, zsh, ksh or sh (as provided by dash), the following should work:</p>
<pre><code>if ! type "$foobar_command_name" > /dev/null; then
# install foobar here
fi
</code></pre>
<p>For a real installation script, you'd probably want to be sure that <code>ty... | <p>In general, that depends on your shell, but if you use bash, zsh, ksh or sh (as provided by dash), the following should work:</p> <pre><code>if ! type "$foobar_command_name" > /dev/null; then # install foobar here fi </code></pre> <p>For a real installation script, you'd probably want to be sure that <code>ty... | 390 | shell | <h1>How can I check if a command exists in a shell script?</h1>
<p>I am writing my first shell script. In my script I would like to check if a certain command exists, and if not, install the executable. How would I check if this command exists?</p>
<pre><code>if # Check that foobar command doesnt exist
then
# Now ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 583 | bash | # How can I check if a command exists in a shell script?
I am writing my first shell script. In my script I would like to check if a certain command exists, and if not, install the executable. How would I check if this command exists?
```
if # Check that foobar command doesnt exist
then
# Now install foobar
fi
``... | In general, that depends on your shell, but if you use bash, zsh, ksh or sh (as provided by dash), the following should work:
```
if ! type "$foobar_command_name" > /dev/null; then
# install foobar here
fi
```
For a real installation script, you'd probably want to be sure that `type` doesn't return successfully in ... |
15374752 | Get yesterday's date in bash on Linux, DST-safe | 220 | 2013-03-13 00:17:51 | <p>I have a shell script that runs on Linux and uses this call to get yesterday's date in <code>YYYY-MM-DD</code> format:</p>
<pre><code>date -d "1 day ago" '+%Y-%m-%d'
</code></pre>
<p>It works most of the time, but when the script ran yesterday morning at <code>2013-03-11 0:35 CDT</code> it returned <code>"2013-03-... | 246,539 | 233,196 | 2023-09-14 06:16:11 | 15,374,813 | 345 | 2013-03-13 00:23:13 | 1,394,729 | 2017-03-28 08:30:08 | https://stackoverflow.com/q/15374752 | https://stackoverflow.com/a/15374813 | <p>I think this should work, irrespective of how often and when you run it ... </p>
<pre><code>date -d "yesterday 13:00" '+%Y-%m-%d'
</code></pre>
| <p>I think this should work, irrespective of how often and when you run it ... </p> <pre><code>date -d "yesterday 13:00" '+%Y-%m-%d' </code></pre> | 58, 387 | bash, linux | <h1>Get yesterday's date in bash on Linux, DST-safe</h1>
<p>I have a shell script that runs on Linux and uses this call to get yesterday's date in <code>YYYY-MM-DD</code> format:</p>
<pre><code>date -d "1 day ago" '+%Y-%m-%d'
</code></pre>
<p>It works most of the time, but when the script ran yesterday morning at <co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 584 | bash | # Get yesterday's date in bash on Linux, DST-safe
I have a shell script that runs on Linux and uses this call to get yesterday's date in `YYYY-MM-DD` format:
```
date -d "1 day ago" '+%Y-%m-%d'
```
It works most of the time, but when the script ran yesterday morning at `2013-03-11 0:35 CDT` it returned `"2013-03-09"... | I think this should work, irrespective of how often and when you run it ...
```
date -d "yesterday 13:00" '+%Y-%m-%d'
``` |
7642674 | How do I script a "yes" response for installing programs? | 205 | 2011-10-04 02:21:00 | <p>I work with <a href="https://en.wikipedia.org/wiki/Amazon_Machine_Image#Amazon_Linux_AMI" rel="noreferrer">Amazon Linux</a> instances and I have a couple scripts to populate data and install all the programs I work with, but a couple of the programs ask:</p>
<pre class="lang-none prettyprint-override"><code>Do you w... | 265,304 | 974,887 | 2024-03-01 07:16:01 | 7,642,711 | 345 | 2011-10-04 02:28:24 | 960,524 | 2021-03-21 03:47:52 | https://stackoverflow.com/q/7642674 | https://stackoverflow.com/a/7642711 | <p>The <a href="http://en.wikipedia.org/wiki/Yes_(Unix)" rel="noreferrer">'yes' command</a> will echo 'y' (or whatever you ask it to) indefinitely. Use it as:</p>
<pre><code>yes | command-that-asks-for-input
</code></pre>
<p>or, if a capital 'Y' is required:</p>
<pre><code>yes Y | command-that-asks-for-input
</code></... | <p>The <a href="http://en.wikipedia.org/wiki/Yes_(Unix)" rel="noreferrer">'yes' command</a> will echo 'y' (or whatever you ask it to) indefinitely. Use it as:</p> <pre><code>yes | command-that-asks-for-input </code></pre> <p>or, if a capital 'Y' is required:</p> <pre><code>yes Y | command-that-asks-for-input </code></... | 58, 387, 531 | bash, linux, scripting | <h1>How do I script a "yes" response for installing programs?</h1>
<p>I work with <a href="https://en.wikipedia.org/wiki/Amazon_Machine_Image#Amazon_Linux_AMI" rel="noreferrer">Amazon Linux</a> instances and I have a couple scripts to populate data and install all the programs I work with, but a couple of the programs ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 585 | bash | # How do I script a "yes" response for installing programs?
I work with [Amazon Linux](https://en.wikipedia.org/wiki/Amazon_Machine_Image#Amazon_Linux_AMI) instances and I have a couple scripts to populate data and install all the programs I work with, but a couple of the programs ask:
```
Do you want to continue [Y/... | The ['yes' command](http://en.wikipedia.org/wiki/Yes_(Unix)) will echo 'y' (or whatever you ask it to) indefinitely. Use it as:
```
yes | command-that-asks-for-input
```
or, if a capital 'Y' is required:
```
yes Y | command-that-asks-for-input
```
---
If you want to pass 'N' you can still use `yes`:
```
yes N | c... |
15993062 | Bash scripting missing ']' | 136 | 2013-04-13 21:18:21 | <p>I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh
I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by removing the ; in line 13 but... | 197,717 | 2,219,023 | 2020-07-24 14:54:03 | 15,993,083 | 345 | 2013-04-13 21:20:25 | 297,323 | 2013-04-13 21:20:25 | https://stackoverflow.com/q/15993062 | https://stackoverflow.com/a/15993083 | <p>Change </p>
<pre><code>if [ -s "p1"]; #line 13
</code></pre>
<p>into</p>
<pre><code>if [ -s "p1" ]; #line 13
</code></pre>
<p>note the space.</p>
| <p>Change </p> <pre><code>if [ -s "p1"]; #line 13 </code></pre> <p>into</p> <pre><code>if [ -s "p1" ]; #line 13 </code></pre> <p>note the space.</p> | 387, 531 | bash, scripting | <h1>Bash scripting missing ']'</h1>
<p>I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh
I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tr... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 586 | bash | # Bash scripting missing ']'
I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh
I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by re... | Change
```
if [ -s "p1"]; #line 13
```
into
```
if [ -s "p1" ]; #line 13
```
note the space. |
2493642 | How can a Linux/Unix Bash script get its own PID? | 233 | 2010-03-22 15:51:29 | <p>I have a script in Bash called <code>Script.sh</code> that needs to know its own PID. In other words, I need to get PID inside <code>Script.sh</code>.</p>
<p>Any idea how to do this?</p>
| 221,913 | 276,285 | 2022-08-05 21:54:41 | 2,493,661 | 344 | 2010-03-22 15:53:45 | 3,333 | 2021-04-22 09:31:11 | https://stackoverflow.com/q/2493642 | https://stackoverflow.com/a/2493661 | <p>The variable <code>$$</code> contains the PID.</p>
| <p>The variable <code>$$</code> contains the PID.</p> | 387, 531, 3300 | bash, pid, scripting | <h1>How can a Linux/Unix Bash script get its own PID?</h1>
<p>I have a script in Bash called <code>Script.sh</code> that needs to know its own PID. In other words, I need to get PID inside <code>Script.sh</code>.</p>
<p>Any idea how to do this?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 587 | bash | # How can a Linux/Unix Bash script get its own PID?
I have a script in Bash called `Script.sh` that needs to know its own PID. In other words, I need to get PID inside `Script.sh`.
Any idea how to do this? | The variable `$$` contains the PID. |
15461737 | How to execute XPath one-liners from shell? | 239 | 2013-03-17 14:16:47 | <p>Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like <code>foo //element@attribute filename.xml</code> or <code>foo //element@attribute < filename.xml</code> and return the results line by line?</p>
<p>I'm looking for something that would a... | 173,270 | 260,122 | 2025-01-03 12:23:45 | 15,461,774 | 343 | 2013-03-17 14:19:46 | 465,183 | 2024-07-24 16:52:56 | https://stackoverflow.com/q/15461737 | https://stackoverflow.com/a/15461774 | <p>You should try these tools :</p>
<ul>
<li><code>xidel</code> (<a href="http://videlibri.sourceforge.net/xidel.html" rel="noreferrer">xidel</a>): xpath3</li>
<li><code>xmlstarlet</code> (<a href="http://xmlstar.sourceforge.net/" rel="noreferrer">xmlstarlet page</a>) : can edit, select, transform... Not installed by d... | <p>You should try these tools :</p> <ul> <li><code>xidel</code> (<a href="http://videlibri.sourceforge.net/xidel.html" rel="noreferrer">xidel</a>): xpath3</li> <li><code>xmlstarlet</code> (<a href="http://xmlstar.sourceforge.net/" rel="noreferrer">xmlstarlet page</a>) : can edit, select, transform... Not installed by d... | 19, 390, 1227, 3285 | cross-platform, shell, xml, xpath | <h1>How to execute XPath one-liners from shell?</h1>
<p>Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like <code>foo //element@attribute filename.xml</code> or <code>foo //element@attribute < filename.xml</code> and return the results line by... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 588 | bash | # How to execute XPath one-liners from shell?
Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like `foo //element@attribute filename.xml` or `foo //element@attribute < filename.xml` and return the results line by line?
I'm looking for something ... | You should try these tools :
- `xidel` ([xidel](http://videlibri.sourceforge.net/xidel.html)): xpath3
- `xmlstarlet` ([xmlstarlet page](http://xmlstar.sourceforge.net/)) : can edit, select, transform... Not installed by default, xpath1
- `xmllint` ([man xmllint](http://xmlsoft.org/xmllint.html)): often installed by de... |
5927489 | Looking for files NOT owned by a specific user | 224 | 2011-05-08 12:43:55 | <p>I'm looking to recursively look through directories to find files NOT owned by a particular user and I am not sure how to write this.</p>
| 129,318 | null | 2021-02-05 00:27:44 | 5,927,523 | 343 | 2011-05-08 12:49:19 | 743,140 | 2011-05-08 12:59:27 | https://stackoverflow.com/q/5927489 | https://stackoverflow.com/a/5927523 | <p>The find(1) utility has primaries that can be negated ("reversed") using the "!" operator. On the prompt one must however escape the negation with a backslash as it is a shell metacharacter. Result:</p>
<pre><code>find . \! -user foo -print
</code></pre>
| <p>The find(1) utility has primaries that can be negated ("reversed") using the "!" operator. On the prompt one must however escape the negation with a backslash as it is a shell metacharacter. Result:</p> <pre><code>find . \! -user foo -print </code></pre> | 58, 390, 816, 5310 | file, linux, search, shell | <h1>Looking for files NOT owned by a specific user</h1>
<p>I'm looking to recursively look through directories to find files NOT owned by a particular user and I am not sure how to write this.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 589 | bash | # Looking for files NOT owned by a specific user
I'm looking to recursively look through directories to find files NOT owned by a particular user and I am not sure how to write this. | The find(1) utility has primaries that can be negated ("reversed") using the "!" operator. On the prompt one must however escape the negation with a backslash as it is a shell metacharacter. Result:
```
find . \! -user foo -print
``` |
392022 | What's the best way to send a signal to all members of a process group? | 472 | 2008-12-24 18:48:00 | <p>I want to kill a whole process tree. What is the best way to do this using any common scripting languages? I am looking for a simple solution.</p>
| 453,981 | 26,658 | 2024-01-06 20:43:20 | 392,155 | 342 | 2008-12-24 20:06:02 | 41,661 | 2016-08-19 22:21:25 | https://stackoverflow.com/q/392022 | https://stackoverflow.com/a/392155 | <p>You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:</p>
<pre><code> ps x -o "%p %r %y %x %c "
</code></pre>
<p>If it is a process ... | <p>You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:</p> <pre><code> ps x -o "%p %r %y %x %c " </code></pre> <p>If it is a process ... | 58, 390, 2481, 5910 | linux, process, shell, signals | <h1>What's the best way to send a signal to all members of a process group?</h1>
<p>I want to kill a whole process tree. What is the best way to do this using any common scripting languages? I am looking for a simple solution.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 590 | bash | # What's the best way to send a signal to all members of a process group?
I want to kill a whole process tree. What is the best way to do this using any common scripting languages? I am looking for a simple solution. | You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
```
ps x -o "%p %r %y %x %c "
```
If it is a process group you want to kill, just ... |
563600 | Can I get "&&" or "-and" to work in PowerShell? | 330 | 2009-02-19 01:34:04 | <p><strong>Note:</strong> This question originally asked in 2009, when powershell did not have support for the <code>&&</code> operator.
In 2019, per Jay's answer, microsoft added support for <code>&&</code> and <code>||</code> in Powershell 7.
<a href="https://stackoverflow.com/a/564092/234">https://st... | 326,251 | 234 | 2025-01-24 07:54:24 | 564,092 | 342 | 2009-02-19 05:51:30 | 5,314 | 2025-01-24 07:54:24 | https://stackoverflow.com/q/563600 | https://stackoverflow.com/a/564092 | <p>In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:</p>
<pre><code>build && run_tests
</code></pre>
<p>In PowerShell, the closest thing you can do is:</p>
<pre><code>(build) -and (run_tests)
</code></pre>
<p>It has the same logic, ... | <p>In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:</p> <pre><code>build && run_tests </code></pre> <p>In PowerShell, the closest thing you can do is:</p> <pre><code>(build) -and (run_tests) </code></pre> <p>It has the same logic, ... | 367, 526 | powershell, syntax | <h1>Can I get "&&" or "-and" to work in PowerShell?</h1>
<p><strong>Note:</strong> This question originally asked in 2009, when powershell did not have support for the <code>&&</code> operator.
In 2019, per Jay's answer, microsoft added support for <code>&&</code> and <code>||</code> in Powershell 7.
<a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 591 | bash | # Can I get "&&" or "-and" to work in PowerShell?
**Note:** This question originally asked in 2009, when powershell did not have support for the `&&` operator.
In 2019, per Jay's answer, microsoft added support for `&&` and `||` in Powershell 7.
<https://stackoverflow.com/a/564092/234>
---
Original Question
`&&` is... | In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:
```
build && run_tests
```
In PowerShell, the closest thing you can do is:
```
(build) -and (run_tests)
```
It has the same logic, but the output text from the commands is lost. Maybe it is good enough for... |
4937792 | Using variables inside a bash heredoc | 291 | 2011-02-08 20:07:06 | <p>I'm trying to interpolate variables inside of a bash heredoc:</p>
<pre><code>var=$1
sudo tee "/path/to/outfile" > /dev/null << "EOF"
Some text that contains my $var
EOF
</code></pre>
<p>This isn't working as I'd expect (<code>$var</code> is treated literally, not expanded).</p>
<p>I need to use <code>sud... | 179,965 | 447,619 | 2024-03-16 18:40:45 | 4,938,197 | 342 | 2011-02-08 20:43:53 | 223,092 | 2022-05-15 11:54:25 | https://stackoverflow.com/q/4937792 | https://stackoverflow.com/a/4938197 | <p>In answer to your first question, there's no parameter substitution because you've put the delimiter in quotes - <a href="https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Here-Documents" rel="noreferrer">the bash manual says</a>:</p>
<blockquote>
<p>The format of here-documents is:</p>
<pre><code> ... | <p>In answer to your first question, there's no parameter substitution because you've put the delimiter in quotes - <a href="https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Here-Documents" rel="noreferrer">the bash manual says</a>:</p> <blockquote> <p>The format of here-documents is:</p> <pre><code> ... | 276, 387, 7449, 10327 | bash, heredoc, sh, variables | <h1>Using variables inside a bash heredoc</h1>
<p>I'm trying to interpolate variables inside of a bash heredoc:</p>
<pre><code>var=$1
sudo tee "/path/to/outfile" > /dev/null << "EOF"
Some text that contains my $var
EOF
</code></pre>
<p>This isn't working as I'd expect (<code>$var</code> is treated literally,... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 592 | bash | # Using variables inside a bash heredoc
I'm trying to interpolate variables inside of a bash heredoc:
```
var=$1
sudo tee "/path/to/outfile" > /dev/null << "EOF"
Some text that contains my $var
EOF
```
This isn't working as I'd expect (`$var` is treated literally, not expanded).
I need to use `sudo tee` because cre... | In answer to your first question, there's no parameter substitution because you've put the delimiter in quotes - [the bash manual says](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Here-Documents):
> The format of here-documents is:
>
> ```
> <<[-]word
> here-document
> de... |
3315389 | How to empty (clear) the logcat buffer in Android | 174 | 2010-07-23 04:25:08 | <p>How can I empty (clear) the logcat buffer in Android?</p>
<p>I use adb logcat from command line and pipe the output to a file, since the DDMS has a very limited buffer. At the moment, when I restart my app (after fixing bugs etc) the logcat buffer has data from the previous launch as well. Even uninstalling the app... | 179,303 | 337,120 | 2020-03-03 23:29:37 | 3,315,407 | 342 | 2010-07-23 04:29:54 | 12,711 | 2016-03-17 18:13:48 | https://stackoverflow.com/q/3315389 | https://stackoverflow.com/a/3315407 | <pre><code>adb logcat -c
</code></pre>
<p>Logcat options are documented here: <a href="http://developer.android.com/tools/help/logcat.html" rel="noreferrer">http://developer.android.com/tools/help/logcat.html</a></p>
| <pre><code>adb logcat -c </code></pre> <p>Logcat options are documented here: <a href="http://developer.android.com/tools/help/logcat.html" rel="noreferrer">http://developer.android.com/tools/help/logcat.html</a></p> | 390, 1386, 22975, 46733 | adb, android, logcat, shell | <h1>How to empty (clear) the logcat buffer in Android</h1>
<p>How can I empty (clear) the logcat buffer in Android?</p>
<p>I use adb logcat from command line and pipe the output to a file, since the DDMS has a very limited buffer. At the moment, when I restart my app (after fixing bugs etc) the logcat buffer has data ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 593 | bash | # How to empty (clear) the logcat buffer in Android
How can I empty (clear) the logcat buffer in Android?
I use adb logcat from command line and pipe the output to a file, since the DDMS has a very limited buffer. At the moment, when I restart my app (after fixing bugs etc) the logcat buffer has data from the previou... | ```
adb logcat -c
```
Logcat options are documented here: <http://developer.android.com/tools/help/logcat.html> |
4708549 | What is the difference between $(command) and `command` in shell programming? | 334 | 2011-01-16 22:34:37 | <p>To store the output of a command as a variable in sh/ksh/bash, you can do either</p>
<pre><code>var=$(command)
</code></pre>
<p>or</p>
<pre><code>var=`command`
</code></pre>
<p>What's the difference if any between the two methods?</p>
| 71,790 | 42,303 | 2025-12-06 16:30:58 | 4,708,569 | 340 | 2011-01-16 22:38:07 | 207,248 | 2017-12-05 20:42:13 | https://stackoverflow.com/q/4708549 | https://stackoverflow.com/a/4708569 | <p>The backticks/gravemarks have been deprecated in favor of <code>$()</code> for command substitution because <code>$()</code> can easily nest within itself as in <code>$(echo foo$(echo bar))</code>. There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. </p>
<p>See <a... | <p>The backticks/gravemarks have been deprecated in favor of <code>$()</code> for command substitution because <code>$()</code> can easily nest within itself as in <code>$(echo foo$(echo bar))</code>. There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. </p> <p>See <a... | 387, 390, 1964, 10327 | bash, ksh, sh, shell | <h1>What is the difference between $(command) and `command` in shell programming?</h1>
<p>To store the output of a command as a variable in sh/ksh/bash, you can do either</p>
<pre><code>var=$(command)
</code></pre>
<p>or</p>
<pre><code>var=`command`
</code></pre>
<p>What's the difference if any between the two meth... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 594 | bash | # What is the difference between $(command) and `command` in shell programming?
To store the output of a command as a variable in sh/ksh/bash, you can do either
```
var=$(command)
```
or
```
var=`command`
```
What's the difference if any between the two methods? | The backticks/gravemarks have been deprecated in favor of `$()` for command substitution because `$()` can easily nest within itself as in `$(echo foo$(echo bar))`. There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc.
See [**BashFAQ/082**](http://mywiki.wooledge.org/Ba... |
18351198 | What are the uses of the exec command in shell scripts? | 314 | 2013-08-21 07:22:49 | <p>Can anyone explain what are the uses of the exec command in shell scripting with simple examples?</p>
| 377,160 | 2,400,564 | 2017-03-19 20:40:38 | 18,351,547 | 340 | 2013-08-21 07:40:28 | 736,308 | 2017-03-19 20:40:38 | https://stackoverflow.com/q/18351198 | https://stackoverflow.com/a/18351547 | <p>The <code>exec</code> built-in command mirrors functions in the kernel, there are a family of them based on <code>execve</code>, which is usually called from C.</p>
<p><code>exec</code> replaces the current program in the current process, without <code>fork</code>ing a new process. It is not something you would us... | <p>The <code>exec</code> built-in command mirrors functions in the kernel, there are a family of them based on <code>execve</code>, which is usually called from C.</p> <p><code>exec</code> replaces the current program in the current process, without <code>fork</code>ing a new process. It is not something you would us... | 34, 390, 7932 | exec, shell, unix | <h1>What are the uses of the exec command in shell scripts?</h1>
<p>Can anyone explain what are the uses of the exec command in shell scripting with simple examples?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 595 | bash | # What are the uses of the exec command in shell scripts?
Can anyone explain what are the uses of the exec command in shell scripting with simple examples? | The `exec` built-in command mirrors functions in the kernel, there are a family of them based on `execve`, which is usually called from C.
`exec` replaces the current program in the current process, without `fork`ing a new process. It is not something you would use in every script you write, but it comes in handy on o... |
8967843 | How do I create a Bash alias? | 278 | 2012-01-23 06:15:33 | <p>I'm on OSX and I need to put something like this, <code>alias blah="/usr/bin/blah"</code> in a config file but I don't know where the config file is. </p>
| 430,910 | 666,564 | 2023-10-06 15:54:24 | 8,967,864 | 340 | 2012-01-23 06:19:23 | 970,195 | 2023-10-06 15:54:24 | https://stackoverflow.com/q/8967843 | https://stackoverflow.com/a/8967864 | <p>You can add an <code>alias</code> or a <code>function</code> in your startup script file.</p>
<h2>MacOS 10.13 High Sierra and earlier:</h2>
<p>The default shell is bash. Usually the startup script file is <code>.bashrc</code>, <code>.bash_login</code> or <code>.profile</code> file in your home directory.</p>
<p>Sin... | <p>You can add an <code>alias</code> or a <code>function</code> in your startup script file.</p> <h2>MacOS 10.13 High Sierra and earlier:</h2> <p>The default shell is bash. Usually the startup script file is <code>.bashrc</code>, <code>.bash_login</code> or <code>.profile</code> file in your home directory.</p> <p>Sin... | 369, 387, 390 | bash, macos, shell | <h1>How do I create a Bash alias?</h1>
<p>I'm on OSX and I need to put something like this, <code>alias blah="/usr/bin/blah"</code> in a config file but I don't know where the config file is. </p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 596 | bash | # How do I create a Bash alias?
I'm on OSX and I need to put something like this, `alias blah="/usr/bin/blah"` in a config file but I don't know where the config file is. | You can add an `alias` or a `function` in your startup script file.
## MacOS 10.13 High Sierra and earlier:
The default shell is bash. Usually the startup script file is `.bashrc`, `.bash_login` or `.profile` file in your home directory.
Since these files are hidden you will have to do an `ls -a` to list them. If yo... |
26475358 | Negate if condition in bash script | 250 | 2014-10-20 21:36:36 | <p>I'm stuck at trying to negate the following command:</p>
<pre><code>wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Sorry you are Offline"
exit 1
</code></pre>
<p>This if condition returns true if I'm connected to the internet. I want it to happen th... | 299,262 | 3,387,528 | 2022-12-29 01:16:18 | 26,475,397 | 340 | 2014-10-20 21:39:33 | 3,776,858 | 2017-05-08 20:51:15 | https://stackoverflow.com/q/26475358 | https://stackoverflow.com/a/26475397 | <p>You can choose:</p>
<pre><code>if [[ $? -ne 0 ]]; then # -ne: not equal
if ! [[ $? -eq 0 ]]; then # -eq: equal
if [[ ! $? -eq 0 ]]; then
</code></pre>
<p><code>!</code> inverts the return of the following expression, respectively.</p>
| <p>You can choose:</p> <pre><code>if [[ $? -ne 0 ]]; then # -ne: not equal if ! [[ $? -eq 0 ]]; then # -eq: equal if [[ ! $? -eq 0 ]]; then </code></pre> <p><code>!</code> inverts the return of the following expression, respectively.</p> | 58, 387, 2773, 19628 | bash, if-statement, linux, negate | <h1>Negate if condition in bash script</h1>
<p>I'm stuck at trying to negate the following command:</p>
<pre><code>wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Sorry you are Offline"
exit 1
</code></pre>
<p>This if condition returns true if I'm conne... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 597 | bash | # Negate if condition in bash script
I'm stuck at trying to negate the following command:
```
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Sorry you are Offline"
exit 1
```
This if condition returns true if I'm connected to the internet. I want it to happen... | You can choose:
```
if [[ $? -ne 0 ]]; then # -ne: not equal
if ! [[ $? -eq 0 ]]; then # -eq: equal
if [[ ! $? -eq 0 ]]; then
```
`!` inverts the return of the following expression, respectively. |
1655372 | Longest line in a file | 259 | 2009-10-31 19:34:36 | <p>I'm looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script.</p>
| 129,483 | 132,401 | 2025-10-02 12:36:51 | 3,191,708 | 337 | 2010-07-07 03:33:06 | 385,124 | 2010-07-07 03:33:06 | https://stackoverflow.com/q/1655372 | https://stackoverflow.com/a/3191708 | <p>Using wc (GNU coreutils) 7.4:</p>
<pre><code>wc -L filename
</code></pre>
<p>gives:</p>
<pre><code>101 filename
</code></pre>
| <p>Using wc (GNU coreutils) 7.4:</p> <pre><code>wc -L filename </code></pre> <p>gives:</p> <pre><code>101 filename </code></pre> | 387, 390, 2189 | bash, shell, utilities | <h1>Longest line in a file</h1>
<p>I'm looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 598 | bash | # Longest line in a file
I'm looking for a simple way to find the length of the longest line in a file. Ideally, it would be a simple bash shell command instead of a script. | Using wc (GNU coreutils) 7.4:
```
wc -L filename
```
gives:
```
101 filename
``` |
44331836 | apt-get install tzdata noninteractive | 244 | 2017-06-02 14:54:31 | <p>When I try to </p>
<pre><code>apt-get install -y tzdata
</code></pre>
<p>the command line option for picking timezone shows up. I am trying to use this in a script to do some setup, how can I make the apt-get run without user input?</p>
<p>I know to reconfigure the tzdata I can do </p>
<pre><code>echo "America/N... | 151,892 | 4,992,422 | 2025-08-29 16:57:56 | 44,333,806 | 337 | 2017-06-02 16:44:44 | 4,992,422 | 2025-08-29 16:57:56 | https://stackoverflow.com/q/44331836 | https://stackoverflow.com/a/44333806 | <p>This is the script I used</p>
<p><strong>(Updated Version with input from @elquimista from the comments)</strong></p>
<pre><code>#!/bin/bash
ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
DEBIAN_FRONTEND=noninteractive apt install -y tzdata
dpkg-reconfigure --frontend noninteractive tzdata
</code></pre>... | <p>This is the script I used</p> <p><strong>(Updated Version with input from @elquimista from the comments)</strong></p> <pre><code>#!/bin/bash ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime DEBIAN_FRONTEND=noninteractive apt install -y tzdata dpkg-reconfigure --frontend noninteractive tzdata </code></pre>... | 387, 549, 16113, 108477 | apt-get, bash, dockerfile, ubuntu | <h1>apt-get install tzdata noninteractive</h1>
<p>When I try to </p>
<pre><code>apt-get install -y tzdata
</code></pre>
<p>the command line option for picking timezone shows up. I am trying to use this in a script to do some setup, how can I make the apt-get run without user input?</p>
<p>I know to reconfigure the t... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 599 | bash | # apt-get install tzdata noninteractive
When I try to
```
apt-get install -y tzdata
```
the command line option for picking timezone shows up. I am trying to use this in a script to do some setup, how can I make the apt-get run without user input?
I know to reconfigure the tzdata I can do
```
echo "America/New_Yor... | This is the script I used
**(Updated Version with input from @elquimista from the comments)**
```
#!/bin/bash
ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
DEBIAN_FRONTEND=noninteractive apt install -y tzdata
dpkg-reconfigure --frontend noninteractive tzdata
```
Seems to work fine.
As one liner:
```
... |
5125907 | How to run a shell script in OS X by double-clicking? | 296 | 2011-02-26 08:41:39 | <p>I have a shell script that has user execution permission on OS X, but when I double click on it, it opens in a text editor. How can I get it to run by double-clicking it?</p>
| 362,460 | 577,436 | 2023-04-12 18:02:58 | 5,126,052 | 336 | 2011-02-26 09:14:33 | 2,187,933 | 2020-08-28 02:40:04 | https://stackoverflow.com/q/5125907 | https://stackoverflow.com/a/5126052 | <ul>
<li><p>First in terminal make the script executable by typing the following command:</p>
<pre><code> chmod a+x yourscriptname
</code></pre>
</li>
<li><p>Then, in Finder, right-click your file and select "Open with" and then "Other...".</p>
</li>
<li><p>Here you select the application you want ... | <ul> <li><p>First in terminal make the script executable by typing the following command:</p> <pre><code> chmod a+x yourscriptname </code></pre> </li> <li><p>Then, in Finder, right-click your file and select "Open with" and then "Other...".</p> </li> <li><p>Here you select the application you want ... | 369, 390, 531, 896 | desktop, macos, scripting, shell | <h1>How to run a shell script in OS X by double-clicking?</h1>
<p>I have a shell script that has user execution permission on OS X, but when I double click on it, it opens in a text editor. How can I get it to run by double-clicking it?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 600 | bash | # How to run a shell script in OS X by double-clicking?
I have a shell script that has user execution permission on OS X, but when I double click on it, it opens in a text editor. How can I get it to run by double-clicking it? | - First in terminal make the script executable by typing the following command:
```
chmod a+x yourscriptname
```
- Then, in Finder, right-click your file and select "Open with" and then "Other...".
- Here you select the application you want the file to execute into, in this case it would be Terminal. To be abl... |
16428098 | Groovy Shell warning "Could not open/create prefs root node ..." | 195 | 2013-05-07 20:35:31 | <p>I tried to open the Groovy Shell (<code>groovysh</code>) on Windows 8 and got the following output:</p>
<pre><code>java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
</code></p... | 160,971 | 158,668 | 2019-11-07 10:25:07 | 17,552,837 | 335 | 2013-07-09 15:58:12 | 2,519,307 | 2019-05-09 05:19:40 | https://stackoverflow.com/q/16428098 | https://stackoverflow.com/a/17552837 | <p>Dennis answer is correct. However I would like to explain the solution in a bit more detailed way (for Windows User):</p>
<ol>
<li>Go into your Start Menu and type <code>regedit</code> into the search field.</li>
<li>Navigate to path <code>HKEY_LOCAL_MACHINE\Software\JavaSoft</code> (Windows 10 seems to now have th... | <p>Dennis answer is correct. However I would like to explain the solution in a bit more detailed way (for Windows User):</p> <ol> <li>Go into your Start Menu and type <code>regedit</code> into the search field.</li> <li>Navigate to path <code>HKEY_LOCAL_MACHINE\Software\JavaSoft</code> (Windows 10 seems to now have th... | 64, 849, 3951 | groovy, groovyshell, windows | <h1>Groovy Shell warning "Could not open/create prefs root node ..."</h1>
<p>I tried to open the Groovy Shell (<code>groovysh</code>) on Windows 8 and got the following output:</p>
<pre><code>java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
at root... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 601 | bash | # Groovy Shell warning "Could not open/create prefs root node ..."
I tried to open the Groovy Shell (`groovysh`) on Windows 8 and got the following output:
```
java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
at root 0x80000002. Windows RegCreateKeyEx(.... | Dennis answer is correct. However I would like to explain the solution in a bit more detailed way (for Windows User):
1. Go into your Start Menu and type `regedit` into the search field.
2. Navigate to path `HKEY_LOCAL_MACHINE\Software\JavaSoft` (Windows 10 seems to now have this here: `HKEY_LOCAL_MACHINE\Software\WOW... |
18062778 | How to hide command output in Bash | 164 | 2013-08-05 16:09:59 | <p>I want to make my Bash scripts more elegant for the end user. How do I hide the output when Bash is executing commands?</p>
<p>For example, when Bash executes</p>
<pre><code>yum install nano
</code></pre>
<p>The following will show up to the user who executed the Bash:</p>
<pre class="lang-none prettyprint-overr... | 328,033 | 2,650,277 | 2022-06-15 16:32:05 | 18,063,720 | 335 | 2013-08-05 17:06:44 | 1,426,891 | 2016-07-07 19:32:50 | https://stackoverflow.com/q/18062778 | https://stackoverflow.com/a/18063720 | <h3>Use this.</h3>
<pre><code>{
/your/first/command
/your/second/command
} &> /dev/null
</code></pre>
<h3>Explanation</h3>
<p>To eliminate output from commands, you have two options:</p>
<ul>
<li><p>Close the output descriptor file, which keeps it from accepting any more input. That looks like this:</p>
... | <h3>Use this.</h3> <pre><code>{ /your/first/command /your/second/command } &> /dev/null </code></pre> <h3>Explanation</h3> <p>To eliminate output from commands, you have two options:</p> <ul> <li><p>Close the output descriptor file, which keeps it from accepting any more input. That looks like this:</p> ... | 387, 390 | bash, shell | <h1>How to hide command output in Bash</h1>
<p>I want to make my Bash scripts more elegant for the end user. How do I hide the output when Bash is executing commands?</p>
<p>For example, when Bash executes</p>
<pre><code>yum install nano
</code></pre>
<p>The following will show up to the user who executed the Bash:<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 602 | bash | # How to hide command output in Bash
I want to make my Bash scripts more elegant for the end user. How do I hide the output when Bash is executing commands?
For example, when Bash executes
```
yum install nano
```
The following will show up to the user who executed the Bash:
```
Loaded plugins: fastestmirror
base ... | ### Use this.
```
{
/your/first/command
/your/second/command
} &> /dev/null
```
### Explanation
To eliminate output from commands, you have two options:
- Close the output descriptor file, which keeps it from accepting any more input. That looks like this:
```
your_command "Is anybody listening?" >&-
```... |
894430 | Creating hard and soft links using PowerShell | 362 | 2009-05-21 18:41:22 | <p>Can PowerShell 1.0 create hard and soft links analogous to the Unix variety? </p>
<p>If this isn't built in, can someone point me to a site that has a ps1 script that mimics this?</p>
<p>This is a necessary function of any good shell, IMHO. :)</p>
| 357,999 | 102,986 | 2019-10-05 10:28:22 | 5,549,583 | 334 | 2011-04-05 09:07:51 | 70,170 | 2019-04-21 06:20:43 | https://stackoverflow.com/q/894430 | https://stackoverflow.com/a/5549583 | <p>You can call the <code>mklink</code> provided by <code>cmd</code>, from PowerShell to make symbolic links:</p>
<pre><code>cmd /c mklink c:\path\to\symlink c:\target\file
</code></pre>
<p>You must pass <code>/d</code> to <code>mklink</code> if the target is a directory.</p>
<pre><code>cmd /c mklink /d c:\path\to\s... | <p>You can call the <code>mklink</code> provided by <code>cmd</code>, from PowerShell to make symbolic links:</p> <pre><code>cmd /c mklink c:\path\to\symlink c:\target\file </code></pre> <p>You must pass <code>/d</code> to <code>mklink</code> if the target is a directory.</p> <pre><code>cmd /c mklink /d c:\path\to\s... | 526, 1122 | powershell, symlink | <h1>Creating hard and soft links using PowerShell</h1>
<p>Can PowerShell 1.0 create hard and soft links analogous to the Unix variety? </p>
<p>If this isn't built in, can someone point me to a site that has a ps1 script that mimics this?</p>
<p>This is a necessary function of any good shell, IMHO. :)</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 603 | bash | # Creating hard and soft links using PowerShell
Can PowerShell 1.0 create hard and soft links analogous to the Unix variety?
If this isn't built in, can someone point me to a site that has a ps1 script that mimics this?
This is a necessary function of any good shell, IMHO. :) | You can call the `mklink` provided by `cmd`, from PowerShell to make symbolic links:
```
cmd /c mklink c:\path\to\symlink c:\target\file
```
You must pass `/d` to `mklink` if the target is a directory.
```
cmd /c mklink /d c:\path\to\symlink c:\target\directory
```
For hard links, I suggest something like [Sysinter... |
2249619 | How to format a DateTime in PowerShell | 265 | 2010-02-12 04:09:59 | <p>I can format the <a href="https://technet.microsoft.com/en-us/library/hh849887.aspx" rel="noreferrer"><code>Get-Date</code></a> cmdlet no problem like this:</p>
<pre><code>$date = Get-Date -format "yyyyMMdd"
</code></pre>
<p>But once I've got <a href="https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs... | 937,818 | 93,733 | 2025-10-10 00:21:28 | 2,249,639 | 334 | 2010-02-12 04:16:24 | 81,769 | 2024-02-16 04:57:22 | https://stackoverflow.com/q/2249619 | https://stackoverflow.com/a/2249639 | <p>The same as you would in <a href="https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings" rel="noreferrer">.NET</a>:</p>
<pre><code>$DateStr = $Date.ToString("yyyyMMdd")
</code></pre>
<p>Or:</p>
<pre><code>$DateStr = '{0:yyyyMMdd}' -f $Date
</code></pre>
<p>Note th... | <p>The same as you would in <a href="https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings" rel="noreferrer">.NET</a>:</p> <pre><code>$DateStr = $Date.ToString("yyyyMMdd") </code></pre> <p>Or:</p> <pre><code>$DateStr = '{0:yyyyMMdd}' -f $Date </code></pre> <p>Note th... | 526, 1263 | datetime, powershell | <h1>How to format a DateTime in PowerShell</h1>
<p>I can format the <a href="https://technet.microsoft.com/en-us/library/hh849887.aspx" rel="noreferrer"><code>Get-Date</code></a> cmdlet no problem like this:</p>
<pre><code>$date = Get-Date -format "yyyyMMdd"
</code></pre>
<p>But once I've got <a href="https://msdn.mi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 604 | bash | # How to format a DateTime in PowerShell
I can format the [`Get-Date`](https://technet.microsoft.com/en-us/library/hh849887.aspx) cmdlet no problem like this:
```
$date = Get-Date -format "yyyyMMdd"
```
But once I've got [a date](https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx) in a varia... | The same as you would in [.NET](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings):
```
$DateStr = $Date.ToString("yyyyMMdd")
```
Or:
```
$DateStr = '{0:yyyyMMdd}' -f $Date
```
Note that you can have a format string that gets multiple objects. For example:
```
'From {... |
5688576 | How to use mod operator in bash? | 238 | 2011-04-16 18:21:47 | <p>I'm trying a line like this:</p>
<pre><code>for i in {1..600}; do wget http://example.com/search/link $i % 5; done;
</code></pre>
<p>What I'm trying to get as output is:</p>
<pre><code>wget http://example.com/search/link0
wget http://example.com/search/link1
wget http://example.com/search/link2
wget http://exampl... | 410,442 | 387,033 | 2025-01-22 16:02:34 | 5,688,592 | 334 | 2011-04-16 18:24:38 | 223,092 | 2018-04-05 17:28:00 | https://stackoverflow.com/q/5688576 | https://stackoverflow.com/a/5688592 | <p>Try the following:</p>
<pre><code> for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done
</code></pre>
<p>The <code>$(( ))</code> syntax does an <a href="http://www.gnu.org/software/bash/manual/bashref.html#Arithmetic-Expansion" rel="noreferrer">arithmetic evaluation</a> of the content... | <p>Try the following:</p> <pre><code> for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done </code></pre> <p>The <code>$(( ))</code> syntax does an <a href="http://www.gnu.org/software/bash/manual/bashref.html#Arithmetic-Expansion" rel="noreferrer">arithmetic evaluation</a> of the content... | 387, 5481, 16642 | arithmetic-expressions, bash, modulo | <h1>How to use mod operator in bash?</h1>
<p>I'm trying a line like this:</p>
<pre><code>for i in {1..600}; do wget http://example.com/search/link $i % 5; done;
</code></pre>
<p>What I'm trying to get as output is:</p>
<pre><code>wget http://example.com/search/link0
wget http://example.com/search/link1
wget http://e... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 605 | bash | # How to use mod operator in bash?
I'm trying a line like this:
```
for i in {1..600}; do wget http://example.com/search/link $i % 5; done;
```
What I'm trying to get as output is:
```
wget http://example.com/search/link0
wget http://example.com/search/link1
wget http://example.com/search/link2
wget http://example.... | Try the following:
```
for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done
```
The `$(( ))` syntax does an [arithmetic evaluation](http://www.gnu.org/software/bash/manual/bashref.html#Arithmetic-Expansion) of the contents. |
26411225 | How to resume scp with partially copied files? | 179 | 2014-10-16 18:16:33 | <p>I'm using the <code>scp</code> shell command to copy huge folder of files.</p>
<p>But at some point in time I had to kill the running command (by Ctrl+C or kill).</p>
<p>To my understanding <code>scp</code> copies files sequentially, so there should be only one partially copied file.</p>
<p><strong>How can same scp ... | 145,755 | 874,275 | 2023-05-12 17:43:43 | 26,412,021 | 334 | 2014-10-16 19:06:15 | 2,464,431 | 2020-08-27 15:46:23 | https://stackoverflow.com/q/26411225 | https://stackoverflow.com/a/26412021 | <p>You should use <code>rsync</code> over <code>ssh</code></p>
<pre><code>rsync -P -e ssh remoteuser@remotehost:/remote/path /local/path
</code></pre>
<p>The key option is <strong><code>-P</code></strong>, which is the same as <code>--partial --progress</code></p>
<blockquote>
<p>By default, rsync will delete any parti... | <p>You should use <code>rsync</code> over <code>ssh</code></p> <pre><code>rsync -P -e ssh remoteuser@remotehost:/remote/path /local/path </code></pre> <p>The key option is <strong><code>-P</code></strong>, which is the same as <code>--partial --progress</code></p> <blockquote> <p>By default, rsync will delete any parti... | 387, 940, 1159, 3149 | bash, resume, rsync, scp | <h1>How to resume scp with partially copied files?</h1>
<p>I'm using the <code>scp</code> shell command to copy huge folder of files.</p>
<p>But at some point in time I had to kill the running command (by Ctrl+C or kill).</p>
<p>To my understanding <code>scp</code> copies files sequentially, so there should be only one... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 606 | bash | # How to resume scp with partially copied files?
I'm using the `scp` shell command to copy huge folder of files.
But at some point in time I had to kill the running command (by Ctrl+C or kill).
To my understanding `scp` copies files sequentially, so there should be only one partially copied file.
**How can same scp... | You should use `rsync` over `ssh`
```
rsync -P -e ssh remoteuser@remotehost:/remote/path /local/path
```
The key option is **`-P`**, which is the same as `--partial --progress`
> By default, rsync will delete any partially transferred file if the transfer is interrupted. In some circumstances it is more desirable to... |
4976658 | On EC2: sudo node command not found, but node without sudo is ok | 139 | 2011-02-12 06:14:07 | <p>I have just installed nodejs on a new EC2 micro instance.</p>
<p>I installed it normally, ./configure -> make -> sudo make install.</p>
<p><strong>Problem:</strong> When I run "node" under ec2-user, it runs perfectly. When I run "sudo node", it fails.</p>
<p>I found out that node is in:</p>
<pre><code>[ec2-user@... | 125,136 | 389,122 | 2022-06-25 09:40:14 | 5,062,718 | 334 | 2011-02-21 05:38:00 | 189,361 | 2011-02-21 05:38:00 | https://stackoverflow.com/q/4976658 | https://stackoverflow.com/a/5062718 | <p>Yes, it is a bit annoying but you can fix it with some links:</p>
<pre><code>sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
</code></pre>
<p>There might be more but that is all... | <p>Yes, it is a bit annoying but you can fix it with some links:</p> <pre><code>sudo ln -s /usr/local/bin/node /usr/bin/node sudo ln -s /usr/local/lib/node /usr/lib/node sudo ln -s /usr/local/bin/npm /usr/bin/npm sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf </code></pre> <p>There might be more but that is all... | 387, 1674, 12375, 46426 | amazon-ec2, bash, node.js, sudo | <h1>On EC2: sudo node command not found, but node without sudo is ok</h1>
<p>I have just installed nodejs on a new EC2 micro instance.</p>
<p>I installed it normally, ./configure -> make -> sudo make install.</p>
<p><strong>Problem:</strong> When I run "node" under ec2-user, it runs perfectly. When I run "sudo node",... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 607 | bash | # On EC2: sudo node command not found, but node without sudo is ok
I have just installed nodejs on a new EC2 micro instance.
I installed it normally, ./configure -> make -> sudo make install.
**Problem:** When I run "node" under ec2-user, it runs perfectly. When I run "sudo node", it fails.
I found out that node is... | Yes, it is a bit annoying but you can fix it with some links:
```
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
```
There might be more but that is all I have run across so far. ... |
4458120 | Search for executable files using find command | 227 | 2010-12-16 06:40:27 | <p>What type of parameter/flag can I use with the Unix <code>find</code> command so that I search executables?</p>
| 237,070 | 507,737 | 2024-09-15 21:51:51 | 4,458,361 | 333 | 2010-12-16 07:26:46 | 90,848 | 2024-02-15 16:22:52 | https://stackoverflow.com/q/4458120 | https://stackoverflow.com/a/4458361 | <p>On GNU versions of find you can use <code>-executable</code>:</p>
<pre><code>find . -type f -executable -print
</code></pre>
<p>For BSD (and MacOS) versions of find, you can use <code>-perm</code> with <code>+</code> and an octal mask:</p>
<pre><code>find . -type f -perm +111 -print
</code></pre>
<p>In this context ... | <p>On GNU versions of find you can use <code>-executable</code>:</p> <pre><code>find . -type f -executable -print </code></pre> <p>For BSD (and MacOS) versions of find, you can use <code>-perm</code> with <code>+</code> and an octal mask:</p> <pre><code>find . -type f -perm +111 -print </code></pre> <p>In this context ... | 34, 387, 391, 10193, 110468 | bash, find, find-util, terminal, unix | <h1>Search for executable files using find command</h1>
<p>What type of parameter/flag can I use with the Unix <code>find</code> command so that I search executables?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 608 | bash | # Search for executable files using find command
What type of parameter/flag can I use with the Unix `find` command so that I search executables? | On GNU versions of find you can use `-executable`:
```
find . -type f -executable -print
```
For BSD (and MacOS) versions of find, you can use `-perm` with `+` and an octal mask:
```
find . -type f -perm +111 -print
```
In this context "+" means "any of these bits are set" and 111 is the execute bits.
Note that th... |
2925606 | How to create a CPU spike with a bash command | 362 | 2010-05-27 22:57:59 | <p>I want to create a near 100% load on a Linux machine. It's quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of time and then stop. I'm hoping there's some trick in bash. I'm thinking some sort of infinite loop. </p>
| 450,983 | 125,380 | 2022-11-04 10:55:16 | 2,927,364 | 332 | 2010-05-28 07:41:42 | 135,960 | 2012-11-04 15:30:35 | https://stackoverflow.com/q/2925606 | https://stackoverflow.com/a/2927364 | <p>You can also do</p>
<pre><code>dd if=/dev/zero of=/dev/null
</code></pre>
<p>To run more of those to put load on more cores, try to fork it: </p>
<pre><code>fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read;... | <p>You can also do</p> <pre><code>dd if=/dev/zero of=/dev/null </code></pre> <p>To run more of those to put load on more cores, try to fork it: </p> <pre><code>fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read;... | 58, 387, 448, 1967 | bash, cpu, linux, load | <h1>How to create a CPU spike with a bash command</h1>
<p>I want to create a near 100% load on a Linux machine. It's quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of time and then stop. I'm hoping there's some trick in bash. I'm thinking some sort of in... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 609 | bash | # How to create a CPU spike with a bash command
I want to create a near 100% load on a Linux machine. It's quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of time and then stop. I'm hoping there's some trick in bash. I'm thinking some sort of infinite loop. | You can also do
```
dd if=/dev/zero of=/dev/null
```
To run more of those to put load on more cores, try to fork it:
```
fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd
```
Repeat the command in the ... |
10238363 | How to get "wc -l" to print just the number of lines without file name? | 242 | 2012-04-19 23:37:29 | <pre><code>wc -l file.txt
</code></pre>
<p>outputs number of lines and file name.</p>
<p>I need just the number itself (not the file name).</p>
<p>I can do this</p>
<pre><code> wc -l file.txt | awk '{print $1}'
</code></pre>
<p>But maybe there is a better way? </p>
| 243,617 | 1,286,528 | 2021-11-17 13:22:18 | 10,239,606 | 332 | 2012-04-20 02:52:03 | 41,661 | 2015-05-28 06:08:15 | https://stackoverflow.com/q/10238363 | https://stackoverflow.com/a/10239606 | <p>Try this way:</p>
<pre><code>wc -l < file.txt
</code></pre>
| <p>Try this way:</p> <pre><code>wc -l < file.txt </code></pre> | 390, 39056 | shell, wc | <h1>How to get "wc -l" to print just the number of lines without file name?</h1>
<pre><code>wc -l file.txt
</code></pre>
<p>outputs number of lines and file name.</p>
<p>I need just the number itself (not the file name).</p>
<p>I can do this</p>
<pre><code> wc -l file.txt | awk '{print $1}'
</code></pre>
<p>But ma... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 610 | bash | # How to get "wc -l" to print just the number of lines without file name?
```
wc -l file.txt
```
outputs number of lines and file name.
I need just the number itself (not the file name).
I can do this
```
wc -l file.txt | awk '{print $1}'
```
But maybe there is a better way? | Try this way:
```
wc -l < file.txt
``` |
19543139 | Bash script processing limited number of commands in parallel | 198 | 2013-10-23 13:33:25 | <p>I have a bash script that looks like this:</p>
<pre><code>#!/bin/bash
wget LINK1 >/dev/null 2>&1
wget LINK2 >/dev/null 2>&1
wget LINK3 >/dev/null 2>&1
wget LINK4 >/dev/null 2>&1
# ..
# ..
wget LINK4000 >/dev/null 2>&1
</code></pre>
<p>But processing each line until... | 212,815 | 1,326,843 | 2018-08-24 17:17:56 | 19,543,185 | 331 | 2013-10-23 13:35:42 | 2,235,132 | 2018-08-24 17:17:56 | https://stackoverflow.com/q/19543139 | https://stackoverflow.com/a/19543185 | <p>Use the <code>wait</code> built-in:</p>
<pre><code>process1 &
process2 &
process3 &
process4 &
wait
process5 &
process6 &
process7 &
process8 &
wait
</code></pre>
<p>For the above example, 4 processes <code>process1</code> ... <code>process4</code> would be started in the background... | <p>Use the <code>wait</code> built-in:</p> <pre><code>process1 & process2 & process3 & process4 & wait process5 & process6 & process7 & process8 & wait </code></pre> <p>For the above example, 4 processes <code>process1</code> ... <code>process4</code> would be started in the background... | 58, 387, 390 | bash, linux, shell | <h1>Bash script processing limited number of commands in parallel</h1>
<p>I have a bash script that looks like this:</p>
<pre><code>#!/bin/bash
wget LINK1 >/dev/null 2>&1
wget LINK2 >/dev/null 2>&1
wget LINK3 >/dev/null 2>&1
wget LINK4 >/dev/null 2>&1
# ..
# ..
wget LINK4000 >... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 611 | bash | # Bash script processing limited number of commands in parallel
I have a bash script that looks like this:
```
#!/bin/bash
wget LINK1 >/dev/null 2>&1
wget LINK2 >/dev/null 2>&1
wget LINK3 >/dev/null 2>&1
wget LINK4 >/dev/null 2>&1
# ..
# ..
wget LINK4000 >/dev/null 2>&1
```
But processing each line until the command... | Use the `wait` built-in:
```
process1 &
process2 &
process3 &
process4 &
wait
process5 &
process6 &
process7 &
process8 &
wait
```
For the above example, 4 processes `process1` ... `process4` would be started in the background, and the shell would wait until those are completed before starting the next set.
From the... |
4321456 | 'find -exec' a shell function in Linux | 279 | 2010-12-01 05:25:34 | <p>Is there a way to get <code>find</code> to execute a function I define in the shell?</p>
<p>For example:</p>
<pre><code>dosomething () {
echo "Doing something with $1"
}
find . -exec dosomething {} \;
</code></pre>
<p>The result of that is:</p>
<pre><code>find: dosomething: No such file or directory
</co... | 116,332 | 303,896 | 2024-07-09 17:53:58 | 4,321,522 | 330 | 2010-12-01 05:38:05 | 9,530 | 2013-02-22 05:13:58 | https://stackoverflow.com/q/4321456 | https://stackoverflow.com/a/4321522 | <p>Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with <code>export -f</code>, otherwise the subshell won't inherit them:</p>
<pre><code>export -f dosomething
find . -exec bash -c 'dosomething "$0"' {} \;
</code></pre>
| <p>Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with <code>export -f</code>, otherwise the subshell won't inherit them:</p> <pre><code>export -f dosomething find . -exec bash -c 'dosomething "$0"' {} \; </code></pre> | 58, 387, 390, 10193 | bash, find, linux, shell | <h1>'find -exec' a shell function in Linux</h1>
<p>Is there a way to get <code>find</code> to execute a function I define in the shell?</p>
<p>For example:</p>
<pre><code>dosomething () {
echo "Doing something with $1"
}
find . -exec dosomething {} \;
</code></pre>
<p>The result of that is:</p>
<pre><code>f... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 612 | bash | # 'find -exec' a shell function in Linux
Is there a way to get `find` to execute a function I define in the shell?
For example:
```
dosomething () {
echo "Doing something with $1"
}
find . -exec dosomething {} \;
```
The result of that is:
```
find: dosomething: No such file or directory
```
Is there a way to g... | Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with `export -f`, otherwise the subshell won't inherit them:
```
export -f dosomething
find . -exec bash -c 'dosomething "$0"' {} \;
``` |
19670061 | bash "if [ false ];" returns true instead of false -- why? | 276 | 2013-10-29 22:04:07 | <p>Why does the following output <code>True</code>?</p>
<pre><code>#!/bin/sh
if [ false ]; then
echo "True"
else
echo "False"
fi
</code></pre>
<p>This will always output <code>True</code> even though the condition would seem to indicate otherwise. If I remove the brackets <code>[]</code> th... | 353,266 | 592,170 | 2025-08-17 19:23:44 | 19,670,094 | 330 | 2013-10-29 22:06:19 | 1,126,841 | 2013-10-29 22:06:19 | https://stackoverflow.com/q/19670061 | https://stackoverflow.com/a/19670094 | <p>You are running the <code>[</code> (aka <code>test</code>) command with the argument "false", not running the command <code>false</code>. Since "false" is a non-empty string, the <code>test</code> command always succeeds. To actually run the command, drop the <code>[</code> command.</p>
<pre><code>if false; then
... | <p>You are running the <code>[</code> (aka <code>test</code>) command with the argument "false", not running the command <code>false</code>. Since "false" is a non-empty string, the <code>test</code> command always succeeds. To actually run the command, drop the <code>[</code> command.</p> <pre><code>if false; then ... | 387, 2848, 15904 | bash, boolean, conditional-operator | <h1>bash "if [ false ];" returns true instead of false -- why?</h1>
<p>Why does the following output <code>True</code>?</p>
<pre><code>#!/bin/sh
if [ false ]; then
echo "True"
else
echo "False"
fi
</code></pre>
<p>This will always output <code>True</code> even though the condition would see... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 613 | bash | # bash "if [ false ];" returns true instead of false -- why?
Why does the following output `True`?
```
#!/bin/sh
if [ false ]; then
echo "True"
else
echo "False"
fi
```
This will always output `True` even though the condition would seem to indicate otherwise. If I remove the brackets `[]` then it works, but... | You are running the `[` (aka `test`) command with the argument "false", not running the command `false`. Since "false" is a non-empty string, the `test` command always succeeds. To actually run the command, drop the `[` command.
```
if false; then
echo "True"
else
echo "False"
fi
``` |
56199111 | Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system | 249 | 2019-05-18 12:51:27 | <p>Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:</p>
<blockquote>
<p>File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.</p>
</blockquote>
<p>After reading <... | 481,318 | 5,148,197 | 2024-01-22 19:31:38 | 56,199,112 | 330 | 2019-05-18 12:51:27 | 5,148,197 | 2023-11-12 00:11:36 | https://stackoverflow.com/q/56199111 | https://stackoverflow.com/a/56199112 | <p><strong>Update August 2021</strong></p>
<p>It seems that <code>terminal.integrated.shellArgs</code> is deprecated. <a href="https://stackoverflow.com/a/67420296/5148197">This</a> answer shows how to do it in new versions.</p>
<p><strong>Original Answer</strong></p>
<p>I found out <a href="https://github.com/Microsof... | <p><strong>Update August 2021</strong></p> <p>It seems that <code>terminal.integrated.shellArgs</code> is deprecated. <a href="https://stackoverflow.com/a/67420296/5148197">This</a> answer shows how to do it in new versions.</p> <p><strong>Original Answer</strong></p> <p>I found out <a href="https://github.com/Microsof... | 526, 1231, 111608 | command-line, powershell, visual-studio-code | <h1>Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system</h1>
<p>Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:</p>
<blockquote>
<p>File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 614 | bash | # Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system
Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:
> File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because run... | **Update August 2021**
It seems that `terminal.integrated.shellArgs` is deprecated. [This](https://stackoverflow.com/a/67420296/5148197) answer shows how to do it in new versions.
**Original Answer**
I found out [here](https://github.com/Microsoft/vscode-python/issues/2559#issuecomment-478381840) that you can add to... |
4967496 | Check if a Windows service exists and delete in PowerShell | 186 | 2011-02-11 09:39:14 | <p>I am currently writing a deployment script that installs a number of Windows services.</p>
<p>The services names are versioned, so I want to delete the prior Windows service version as part of the installs of the new service.</p>
<p>How can I best do this in PowerShell?</p>
| 332,475 | 395,440 | 2023-01-04 21:29:47 | 4,967,750 | 329 | 2011-02-11 10:07:24 | 236,131 | 2018-09-02 06:16:06 | https://stackoverflow.com/q/4967496 | https://stackoverflow.com/a/4967750 | <p>You can use WMI or other tools for this since there is no <code>Remove-Service</code> cmdlet until Powershell 6.0 (<a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service?view=powershell-6&viewFallbackFrom=powershell-5" rel="noreferrer" title="Remove-Service mo... | <p>You can use WMI or other tools for this since there is no <code>Remove-Service</code> cmdlet until Powershell 6.0 (<a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service?view=powershell-6&viewFallbackFrom=powershell-5" rel="noreferrer" title="Remove-Service mo... | 526, 2596 | powershell, windows-services | <h1>Check if a Windows service exists and delete in PowerShell</h1>
<p>I am currently writing a deployment script that installs a number of Windows services.</p>
<p>The services names are versioned, so I want to delete the prior Windows service version as part of the installs of the new service.</p>
<p>How can I best... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 615 | bash | # Check if a Windows service exists and delete in PowerShell
I am currently writing a deployment script that installs a number of Windows services.
The services names are versioned, so I want to delete the prior Windows service version as part of the installs of the new service.
How can I best do this in PowerShell? | You can use WMI or other tools for this since there is no `Remove-Service` cmdlet until Powershell 6.0 ([See Remove-Service doc)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service?view=powershell-6&viewFallbackFrom=powershell-5 "Remove-Service module documentation for Po... |
27920806 | How to avoid heredoc expanding variables? | 165 | 2015-01-13 11:12:54 | <p>I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping</p>
<pre><code>export PLACEHOLDER1="myPlaceholder1Value"
sudo /bin/su -c "cat << EOF > /etc/init.d/my-script
#!/bin/bash
myvariable_1=toto$PLACEHOLDER1
myvariable_final=\"d... | 70,073 | 1,842,490 | 2023-05-04 03:44:12 | 27,921,346 | 329 | 2015-01-13 11:42:06 | 1,983,854 | 2023-05-04 03:44:12 | https://stackoverflow.com/q/27920806 | https://stackoverflow.com/a/27921346 | <p>Just use <code>'EOF'</code> to prevent the variable from expanding:</p>
<pre><code>sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script
# ^ ^
... contents go here
EOF
</code></pre>
<p>From <code>man bash</code>:</p>
<blockquote>
<p><strong>Here Documents</strong></p>
<p>This ty... | <p>Just use <code>'EOF'</code> to prevent the variable from expanding:</p> <pre><code>sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script # ^ ^ ... contents go here EOF </code></pre> <p>From <code>man bash</code>:</p> <blockquote> <p><strong>Here Documents</strong></p> <p>This ty... | 387, 4804, 7449, 16107 | bash, escaping, heredoc, substitution | <h1>How to avoid heredoc expanding variables?</h1>
<p>I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping</p>
<pre><code>export PLACEHOLDER1="myPlaceholder1Value"
sudo /bin/su -c "cat << EOF > /etc/init.d/my-script
#!/bin/bash
myvar... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 616 | bash | # How to avoid heredoc expanding variables?
I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping
```
export PLACEHOLDER1="myPlaceholder1Value"
sudo /bin/su -c "cat << EOF > /etc/init.d/my-script
#!/bin/bash
myvariable_1=toto$PLACEHOLDER1
myvariable_final=\... | Just use `'EOF'` to prevent the variable from expanding:
```
sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script
# ^ ^
... contents go here
EOF
```
From `man bash`:
> **Here Documents**
>
> This type of redirection instructs the shell to read input from the
> current source until a line con... |
29045140 | env: bash\r: No such file or directory | 225 | 2015-03-14 03:33:11 | <p>I'm trying to install YouCompleteMe from <a href="http://christopherpoole.github.io/setting-up-vim-with-YouCompleteMe/" rel="noreferrer">here</a>.</p>
<p>When I execute:</p>
<pre class="lang-sh prettyprint-override"><code>./install.sh --clang-completer
</code></pre>
<p>I get this error:</p>
<pre class="lang-sh prett... | 386,799 | 2,796,878 | 2023-08-02 15:59:32 | 29,045,187 | 328 | 2015-03-14 03:41:09 | 45,375 | 2023-08-02 15:59:32 | https://stackoverflow.com/q/29045140 | https://stackoverflow.com/a/29045187 | <p>The error message suggests that <strong>the script you're invoking has embedded <code>\r</code> characters</strong>, which in turn suggests that it has <strong>Windows-style <code>\r\n</code> line endings</strong> (newlines) instead of the <code>\n</code>-only line endings <code>bash</code> expects.</p>
<p>As a <str... | <p>The error message suggests that <strong>the script you're invoking has embedded <code>\r</code> characters</strong>, which in turn suggests that it has <strong>Windows-style <code>\r\n</code> line endings</strong> (newlines) instead of the <code>\n</code>-only line endings <code>bash</code> expects.</p> <p>As a <str... | 64, 119, 387, 8329, 59139 | bash, core.autocrlf, git, line-endings, windows | <h1>env: bash\r: No such file or directory</h1>
<p>I'm trying to install YouCompleteMe from <a href="http://christopherpoole.github.io/setting-up-vim-with-YouCompleteMe/" rel="noreferrer">here</a>.</p>
<p>When I execute:</p>
<pre class="lang-sh prettyprint-override"><code>./install.sh --clang-completer
</code></pre>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 617 | bash | # env: bash\r: No such file or directory
I'm trying to install YouCompleteMe from [here](http://christopherpoole.github.io/setting-up-vim-with-YouCompleteMe/).
When I execute:
```
./install.sh --clang-completer
```
I get this error:
```
env: bash\r: No such file or directory
```
I don't know what's wrong with env... | The error message suggests that **the script you're invoking has embedded `\r` characters**, which in turn suggests that it has **Windows-style `\r\n` line endings** (newlines) instead of the `\n`-only line endings `bash` expects.
As a **quick fix**, you can remove the `\r` chars. as follows:
```
sed $'s/\r$//' ./ins... |
284662 | How do you normalize a file path in Bash? | 314 | 2008-11-12 17:16:06 | <p>I want to transform <code>/foo/bar/..</code> to <code>/foo</code></p>
<p>Is there a bash command which does this?</p>
<hr>
<p>Edit: in my practical case, the directory does exist.</p>
| 216,277 | 21,132 | 2024-07-05 16:22:55 | 284,671 | 327 | 2008-11-12 17:20:40 | 15,614 | 2018-08-07 21:38:49 | https://stackoverflow.com/q/284662 | https://stackoverflow.com/a/284671 | <p>if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too. </p>
<pre><code>dirname /foo/bar/baz
# /foo/bar
basename /foo/bar/baz
# baz
dirname $( dirname /foo/bar/baz )
# /foo
realpath ../foo
# ../foo: No such file or directory
realpath... | <p>if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too. </p> <pre><code>dirname /foo/bar/baz # /foo/bar basename /foo/bar/baz # baz dirname $( dirname /foo/bar/baz ) # /foo realpath ../foo # ../foo: No such file or directory realpath... | 34, 58, 387, 390 | bash, linux, shell, unix | <h1>How do you normalize a file path in Bash?</h1>
<p>I want to transform <code>/foo/bar/..</code> to <code>/foo</code></p>
<p>Is there a bash command which does this?</p>
<hr>
<p>Edit: in my practical case, the directory does exist.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 618 | bash | # How do you normalize a file path in Bash?
I want to transform `/foo/bar/..` to `/foo`
Is there a bash command which does this?
---
Edit: in my practical case, the directory does exist. | if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too.
```
dirname /foo/bar/baz
# /foo/bar
basename /foo/bar/baz
# baz
dirname $( dirname /foo/bar/baz )
# /foo
realpath ../foo
# ../foo: No such file or directory
realpath /tmp/../tmp/..... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.