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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
18215973 | How to check if running as root in a bash script | 479 | 2013-08-13 17:56:37 | <p>I'm writing a script that requires root level permissions, and I want to make it so that if the script is not run as root, it simply echoes "Please run as root." and exits.</p>
<p>Here's some pseudocode for what I'm looking for:</p>
<pre><code>if (whoami != root)
then echo "Please run as root"
else (do stuff)... | 417,072 | 954,986 | 2025-05-22 10:06:25 | 28,776,100 | 223 | 2015-02-27 23:29:34 | 954,986 | 2024-08-10 12:42:56 | https://stackoverflow.com/q/18215973 | https://stackoverflow.com/a/28776100 | <p>A few answers have been given, but it appears that the best method is to use is:</p>
<pre><code>id -u
</code></pre>
<p>If run as root, this command will return an id of 0.</p>
<p>This appears to be more reliable than the other methods, and it seems that it return an id of 0 even if the script is run through <code>su... | <p>A few answers have been given, but it appears that the best method is to use is:</p> <pre><code>id -u </code></pre> <p>If run as root, this command will return an id of 0.</p> <p>This appears to be more reliable than the other methods, and it seems that it return an id of 0 even if the script is run through <code>su... | 387, 390, 12079 | bash, root, shell | <h1>How to check if running as root in a bash script</h1>
<p>I'm writing a script that requires root level permissions, and I want to make it so that if the script is not run as root, it simply echoes "Please run as root." and exits.</p>
<p>Here's some pseudocode for what I'm looking for:</p>
<pre><code>if (whoami !=... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 921 | bash | # How to check if running as root in a bash script
I'm writing a script that requires root level permissions, and I want to make it so that if the script is not run as root, it simply echoes "Please run as root." and exits.
Here's some pseudocode for what I'm looking for:
```
if (whoami != root)
then echo "Please ... | A few answers have been given, but it appears that the best method is to use is:
```
id -u
```
If run as root, this command will return an id of 0.
This appears to be more reliable than the other methods, and it seems that it return an id of 0 even if the script is run through `sudo`.
Here is an example of checking... |
630372 | Determine the path of the executing Bash script | 151 | 2009-03-10 14:10:22 | <p>In a Windows command script, one can determine the <em>directory</em> path of the currently executing script using <code>%~dp0</code>. For example:</p>
<pre><code>@echo Running from %~dp0
</code></pre>
<p>What would be the equivalent in a <a href="https://www.gnu.org/software/bash/" rel="noreferrer">Bash</a> script?... | 198,432 | 6,682 | 2022-12-18 16:05:32 | 630,387 | 223 | 2009-03-10 14:12:26 | 66,516 | 2022-11-10 03:54:28 | https://stackoverflow.com/q/630372 | https://stackoverflow.com/a/630387 | <p>For the relative path (i.e. the direct equivalent of Windows' <code>%~dp0</code>):</p>
<pre><code>MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")"
echo "$MY_PATH"
</code></pre>
<p>For the absolute, normalized path:</p>
<pre><code>MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")"... | <p>For the relative path (i.e. the direct equivalent of Windows' <code>%~dp0</code>):</p> <pre><code>MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")" echo "$MY_PATH" </code></pre> <p>For the absolute, normalized path:</p> <pre><code>MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")"... | 387 | bash | <h1>Determine the path of the executing Bash script</h1>
<p>In a Windows command script, one can determine the <em>directory</em> path of the currently executing script using <code>%~dp0</code>. For example:</p>
<pre><code>@echo Running from %~dp0
</code></pre>
<p>What would be the equivalent in a <a href="https://www.... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 922 | bash | # Determine the path of the executing Bash script
In a Windows command script, one can determine the *directory* path of the currently executing script using `%~dp0`. For example:
```
@echo Running from %~dp0
```
What would be the equivalent in a [Bash](https://www.gnu.org/software/bash/) script? | For the relative path (i.e. the direct equivalent of Windows' `%~dp0`):
```
MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")"
echo "$MY_PATH"
```
For the absolute, normalized path:
```
MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")" # relative
MY_PATH="$(cd -- "$MY_PATH" && pwd)" # absolutized and normalized
if ... |
32337643 | How can I run a C program on Mac OS X using Terminal? | 124 | 2015-09-01 17:31:04 | <p>I am new to C. Here is my "Hello, World!" program.</p>
<pre><code>#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
</code></pre>
<p>After I try to run it using Terminal it says:</p>
<pre class="lang-none prettyprint-override"><code>/Users/macbook/Desktop/peng/... | 408,684 | 5,289,524 | 2022-11-16 11:56:29 | 32,338,889 | 223 | 2015-09-01 18:50:07 | 2,836,621 | 2022-05-10 20:35:53 | https://stackoverflow.com/q/32337643 | https://stackoverflow.com/a/32338889 | <p>First save your program as <code>program.c</code>.</p>
<p>Now you need the compiler, so you need to go to <strong>App Store</strong> and install <strong>Xcode</strong> which is Apple's compiler and development tools. How can you find <strong>App Store</strong>? Do a <em>"Spotlight Search"</em> by typing <k... | <p>First save your program as <code>program.c</code>.</p> <p>Now you need the compiler, so you need to go to <strong>App Store</strong> and install <strong>Xcode</strong> which is Apple's compiler and development tools. How can you find <strong>App Store</strong>? Do a <em>"Spotlight Search"</em> by typing <k... | 8, 369, 387 | bash, c, macos | <h1>How can I run a C program on Mac OS X using Terminal?</h1>
<p>I am new to C. Here is my "Hello, World!" program.</p>
<pre><code>#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
</code></pre>
<p>After I try to run it using Terminal it says:</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 | 923 | bash | # How can I run a C program on Mac OS X using Terminal?
I am new to C. Here is my "Hello, World!" program.
```
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
```
After I try to run it using Terminal it says:
```
/Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/pe... | First save your program as `program.c`.
Now you need the compiler, so you need to go to **App Store** and install **Xcode** which is Apple's compiler and development tools. How can you find **App Store**? Do a *"Spotlight Search"* by typing `⌘``Space` and start typing `App Store` and hit `Enter` when it guesses correc... |
16553089 | Dynamic variable names in Bash | 321 | 2013-05-14 21:22:48 | <p>I am confused about a bash script.</p>
<p>I have the following code:</p>
<pre><code>function grep_search() {
magic_way_to_define_magic_variable_$1=`ls | tail -1`
echo $magic_variable_$1
}
</code></pre>
<p>I want to be able to create a variable name containing the first argument of the command and bearing ... | 396,652 | 2,383,408 | 2025-02-25 11:17:26 | 16,553,351 | 222 | 2013-05-14 21:43:30 | 1,126,841 | 2018-04-13 18:19:32 | https://stackoverflow.com/q/16553089 | https://stackoverflow.com/a/16553351 | <p>Use an associative array, with command names as keys.</p>
<pre><code># Requires bash 4, though
declare -A magic_variable=()
function grep_search() {
magic_variable[$1]=$( ls | tail -1 )
echo ${magic_variable[$1]}
}
</code></pre>
<p>If you can't use associative arrays (e.g., you must support <code>bash</co... | <p>Use an associative array, with command names as keys.</p> <pre><code># Requires bash 4, though declare -A magic_variable=() function grep_search() { magic_variable[$1]=$( ls | tail -1 ) echo ${magic_variable[$1]} } </code></pre> <p>If you can't use associative arrays (e.g., you must support <code>bash</co... | 276, 367, 387, 6828 | bash, dynamic, syntax, variables | <h1>Dynamic variable names in Bash</h1>
<p>I am confused about a bash script.</p>
<p>I have the following code:</p>
<pre><code>function grep_search() {
magic_way_to_define_magic_variable_$1=`ls | tail -1`
echo $magic_variable_$1
}
</code></pre>
<p>I want to be able to create a variable name containing the fi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 924 | bash | # Dynamic variable names in Bash
I am confused about a bash script.
I have the following code:
```
function grep_search() {
magic_way_to_define_magic_variable_$1=`ls | tail -1`
echo $magic_variable_$1
}
```
I want to be able to create a variable name containing the first argument of the command and bearing ... | Use an associative array, with command names as keys.
```
# Requires bash 4, though
declare -A magic_variable=()
function grep_search() {
magic_variable[$1]=$( ls | tail -1 )
echo ${magic_variable[$1]}
}
```
If you can't use associative arrays (e.g., you must support `bash` 3), you can use `declare` to creat... |
114814 | count (non-blank) lines-of-code in bash | 182 | 2008-09-22 13:20:42 | <p>In Bash, how do I count the number of non-blank lines of code in a project?</p>
| 164,974 | 10,176 | 2024-03-22 10:40:51 | 114,836 | 222 | 2008-09-22 13:23:10 | 1,496,728 | 2008-09-22 13:37:39 | https://stackoverflow.com/q/114814 | https://stackoverflow.com/a/114836 | <pre><code>cat foo.c | sed '/^\s*$/d' | wc -l
</code></pre>
<p>And if you consider comments blank lines:</p>
<pre><code>cat foo.pl | sed '/^\s*#/d;/^\s*$/d' | wc -l
</code></pre>
<p>Although, that's language dependent. </p>
| <pre><code>cat foo.c | sed '/^\s*$/d' | wc -l </code></pre> <p>And if you consider comments blank lines:</p> <pre><code>cat foo.pl | sed '/^\s*#/d;/^\s*$/d' | wc -l </code></pre> <p>Although, that's language dependent. </p> | 34, 387, 6366, 7989, 7990 | bash, count, lines, nonblank, unix | <h1>count (non-blank) lines-of-code in bash</h1>
<p>In Bash, how do I count the number of non-blank lines of code in a project?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 925 | bash | # count (non-blank) lines-of-code in bash
In Bash, how do I count the number of non-blank lines of code in a project? | ```
cat foo.c | sed '/^\s*$/d' | wc -l
```
And if you consider comments blank lines:
```
cat foo.pl | sed '/^\s*#/d;/^\s*$/d' | wc -l
```
Although, that's language dependent. |
1809899 | How can I assign the output of a function to a variable using bash? | 152 | 2009-11-27 17:36:08 | <p>I have a bash function that produces some output:</p>
<pre><code>function scan {
echo "output"
}
</code></pre>
<p><strong>How can I assign this output to a variable?</strong></p>
<p>ie. <code>VAR=scan</code> (of course this doesn't work - it makes <code>VAR</code> equal the string <code>"scan"<... | 188,435 | 3,764 | 2024-10-22 20:33:24 | 1,809,910 | 222 | 2009-11-27 17:37:57 | 213,320 | 2022-11-07 18:48:34 | https://stackoverflow.com/q/1809899 | https://stackoverflow.com/a/1809910 | <pre><code>VAR=$(scan)
</code></pre>
<p>Exactly the same way as for programs.</p>
| <pre><code>VAR=$(scan) </code></pre> <p>Exactly the same way as for programs.</p> | 276, 387, 5569 | bash, function, variables | <h1>How can I assign the output of a function to a variable using bash?</h1>
<p>I have a bash function that produces some output:</p>
<pre><code>function scan {
echo "output"
}
</code></pre>
<p><strong>How can I assign this output to a variable?</strong></p>
<p>ie. <code>VAR=scan</code> (of course this does... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 926 | bash | # How can I assign the output of a function to a variable using bash?
I have a bash function that produces some output:
```
function scan {
echo "output"
}
```
**How can I assign this output to a variable?**
ie. `VAR=scan` (of course this doesn't work - it makes `VAR` equal the string `"scan"`) | ```
VAR=$(scan)
```
Exactly the same way as for programs. |
3401183 | Bash syntax error: "[[: not found" | 106 | 2010-08-03 22:15:16 | <p>I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.</p>
<p>Here's what's happening:
I have a skeleton script that calls another script that does most of the work. However, it makes calls t... | 135,998 | 410,194 | 2018-06-20 21:55:48 | 3,401,197 | 222 | 2010-08-03 22:17:48 | 26,428 | 2015-07-31 09:25:56 | https://stackoverflow.com/q/3401183 | https://stackoverflow.com/a/3401197 | <p>If your script is executable and you are executing it like <code>./getconfig.sh</code>, the first line of your script needs to be:</p>
<pre><code>#!/bin/bash
</code></pre>
<p>Without that shebang line, your script will be interpreted by <code>sh</code> which doesn't understand <code>[[</code> in <code>if</code> st... | <p>If your script is executable and you are executing it like <code>./getconfig.sh</code>, the first line of your script needs to be:</p> <pre><code>#!/bin/bash </code></pre> <p>Without that shebang line, your script will be interpreted by <code>sh</code> which doesn't understand <code>[[</code> in <code>if</code> st... | 387 | bash | <h1>Bash syntax error: "[[: not found"</h1>
<p>I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.</p>
<p>Here's what's happening:
I have a skeleton script that calls another script that does... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 927 | bash | # Bash syntax error: "[[: not found"
I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.
Here's what's happening:
I have a skeleton script that calls another script that does most of the wor... | If your script is executable and you are executing it like `./getconfig.sh`, the first line of your script needs to be:
```
#!/bin/bash
```
Without that shebang line, your script will be interpreted by `sh` which doesn't understand `[[` in `if` statements.
Otherwise, you should run your script like `bash getconfig.s... |
2327191 | Preserve colouring after piping grep to grep | 206 | 2010-02-24 15:36:33 | <p>There is a simlar question in <a href="https://stackoverflow.com/questions/867877/preserve-ls-colouring-after-greping">Preserve ls colouring after grep’ing</a> but it annoys me that if you pipe colored grep output into another grep that the coloring is not preserved.</p>
<p>As an example <code>grep --color WORD * |... | 65,291 | 11,722 | 2020-08-17 20:33:59 | 2,327,216 | 221 | 2010-02-24 15:38:49 | 92,493 | 2010-02-25 01:38:22 | https://stackoverflow.com/q/2327191 | https://stackoverflow.com/a/2327216 | <p><code>grep</code> sometimes disables the color output, for example when writing to a pipe. You can override this behavior with <code>grep --color=always</code> </p>
<p>The correct command line would be </p>
<pre><code>grep --color=always WORD * | grep -v AVOID
</code></pre>
<p>This is pretty verbose, alternativel... | <p><code>grep</code> sometimes disables the color output, for example when writing to a pipe. You can override this behavior with <code>grep --color=always</code> </p> <p>The correct command line would be </p> <pre><code>grep --color=always WORD * | grep -v AVOID </code></pre> <p>This is pretty verbose, alternativel... | 58, 387, 1271, 5813, 6439 | bash, colors, grep, linux, pipe | <h1>Preserve colouring after piping grep to grep</h1>
<p>There is a simlar question in <a href="https://stackoverflow.com/questions/867877/preserve-ls-colouring-after-greping">Preserve ls colouring after grep’ing</a> but it annoys me that if you pipe colored grep output into another grep that the coloring is not preser... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 928 | bash | # Preserve colouring after piping grep to grep
There is a simlar question in [Preserve ls colouring after grep’ing](https://stackoverflow.com/questions/867877/preserve-ls-colouring-after-greping) but it annoys me that if you pipe colored grep output into another grep that the coloring is not preserved.
As an example ... | `grep` sometimes disables the color output, for example when writing to a pipe. You can override this behavior with `grep --color=always`
The correct command line would be
```
grep --color=always WORD * | grep -v AVOID
```
This is pretty verbose, alternatively you can just add the line
```
alias cgrep="grep --color... |
10121182 | Multi-line bash commands in makefile | 191 | 2012-04-12 09:47:24 | <p>Considering that every command is run in its own shell, what is the best way to run a multi-line bash command in a makefile? For example, like this:</p>
<pre><code>for i in `find`
do
all="$all $i"
done
gcc $all
</code></pre>
| 236,605 | 897,090 | 2024-10-28 18:13:37 | 10,121,575 | 221 | 2012-04-12 10:13:15 | 545,027 | 2019-04-15 11:50:16 | https://stackoverflow.com/q/10121182 | https://stackoverflow.com/a/10121575 | <p>You can use backslash for line continuation. However note that the shell receives the whole command concatenated into a single line, so you also need to terminate some of the lines with a semicolon:</p>
<pre><code>foo:
for i in `find`; \
do \
all="$$all $$i"; \
done; ... | <p>You can use backslash for line continuation. However note that the shell receives the whole command concatenated into a single line, so you also need to terminate some of the lines with a semicolon:</p> <pre><code>foo: for i in `find`; \ do \ all="$$all $$i"; \ done; ... | 387, 4301 | bash, makefile | <h1>Multi-line bash commands in makefile</h1>
<p>Considering that every command is run in its own shell, what is the best way to run a multi-line bash command in a makefile? For example, like this:</p>
<pre><code>for i in `find`
do
all="$all $i"
done
gcc $all
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 929 | bash | # Multi-line bash commands in makefile
Considering that every command is run in its own shell, what is the best way to run a multi-line bash command in a makefile? For example, like this:
```
for i in `find`
do
all="$all $i"
done
gcc $all
``` | You can use backslash for line continuation. However note that the shell receives the whole command concatenated into a single line, so you also need to terminate some of the lines with a semicolon:
```
foo:
for i in `find`; \
do \
all="$$all $$i"; \
done; \
... |
29378566 | I just assigned a variable, but echo $variable shows something else | 179 | 2015-03-31 21:05:46 | <p>Here are a series of cases where <code>echo $var</code> can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or unquoted. </p>
<p>How do I get the shell to set my variable correctly?</p>
<p><strong>Asterisks</strong></p>
... | 476,885 | 1,899,640 | 2024-06-20 10:18:27 | 29,378,567 | 221 | 2015-03-31 21:05:46 | 1,899,640 | 2015-03-31 21:05:46 | https://stackoverflow.com/q/29378566 | https://stackoverflow.com/a/29378567 | <p>In all of the cases above, the variable is correctly set, but not correctly read! The right way is to <strong>use double quotes <em>when referencing</em></strong>:</p>
<pre><code>echo "$var"
</code></pre>
<p>This gives the expected value in all the examples given. Always quote variable references!</p>
<hr>
<p>Wh... | <p>In all of the cases above, the variable is correctly set, but not correctly read! The right way is to <strong>use double quotes <em>when referencing</em></strong>:</p> <pre><code>echo "$var" </code></pre> <p>This gives the expected value in all the examples given. Always quote variable references!</p> <hr> <p>Wh... | 387, 390, 10327, 10804 | bash, quoting, sh, shell | <h1>I just assigned a variable, but echo $variable shows something else</h1>
<p>Here are a series of cases where <code>echo $var</code> can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or unquoted. </p>
<p>How do I get th... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 930 | bash | # I just assigned a variable, but echo $variable shows something else
Here are a series of cases where `echo $var` can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or unquoted.
How do I get the shell to set my variable c... | In all of the cases above, the variable is correctly set, but not correctly read! The right way is to **use double quotes *when referencing***:
```
echo "$var"
```
This gives the expected value in all the examples given. Always quote variable references!
---
Why?
When a variable is **unquoted**, it will:
1. Under... |
20551566 | Display current date and time without punctuation | 155 | 2013-12-12 18:39:57 | <p>For example, I want to display current date and time as the following format:</p>
<pre><code>yyyymmddhhmmss
</code></pre>
<p>How do I do that? It seems like most date format comes with <code>-</code>, <code>/</code>, <code>:</code>, etc.</p>
| 176,737 | 1,454,510 | 2018-08-11 17:06:19 | 20,551,593 | 221 | 2013-12-12 18:41:05 | 641,955 | 2018-05-15 10:13:33 | https://stackoverflow.com/q/20551566 | https://stackoverflow.com/a/20551593 | <p>Here you go:</p>
<pre><code>date +%Y%m%d%H%M%S
</code></pre>
<p>As <code>man date</code> says near the top, you can use the <code>date</code> command like this:</p>
<blockquote>
<pre><code>date [OPTION]... [+FORMAT]
</code></pre>
</blockquote>
<p>That is, you can give it a format parameter, starting with a <code... | <p>Here you go:</p> <pre><code>date +%Y%m%d%H%M%S </code></pre> <p>As <code>man date</code> says near the top, you can use the <code>date</code> command like this:</p> <blockquote> <pre><code>date [OPTION]... [+FORMAT] </code></pre> </blockquote> <p>That is, you can give it a format parameter, starting with a <code... | 387, 390, 5002 | bash, date, shell | <h1>Display current date and time without punctuation</h1>
<p>For example, I want to display current date and time as the following format:</p>
<pre><code>yyyymmddhhmmss
</code></pre>
<p>How do I do that? It seems like most date format comes with <code>-</code>, <code>/</code>, <code>:</code>, etc.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 931 | bash | # Display current date and time without punctuation
For example, I want to display current date and time as the following format:
```
yyyymmddhhmmss
```
How do I do that? It seems like most date format comes with `-`, `/`, `:`, etc. | Here you go:
```
date +%Y%m%d%H%M%S
```
As `man date` says near the top, you can use the `date` command like this:
> ```
> date [OPTION]... [+FORMAT]
> ```
That is, you can give it a format parameter, starting with a `+`.
You can probably guess the meaning of the formatting symbols I used:
- `%Y` is for year
- `%m... |
3043978 | How to check if a process id (PID) exists | 274 | 2010-06-15 09:30:26 | <p>In a bash script, I want to do the following (in pseudo-code):</p>
<pre><code>if [ a process exists with $PID ]; then
kill $PID
fi
</code></pre>
<p>What's the appropriate expression for the conditional statement?</p>
| 427,160 | 183,579 | 2022-10-10 14:15:55 | 3,044,045 | 220 | 2010-06-15 09:40:23 | 233,014 | 2021-10-28 14:24:19 | https://stackoverflow.com/q/3043978 | https://stackoverflow.com/a/3044045 | <p>To check for the existence of a process, use</p>
<pre><code>kill -0 $pid
</code></pre>
<p>But just as <a href="https://stackoverflow.com/a/3043991/4575793">@unwind said</a>, if you want it to terminate in any case, then just</p>
<pre><code>kill $pid
</code></pre>
<p>Otherwise you will have a race condition, where th... | <p>To check for the existence of a process, use</p> <pre><code>kill -0 $pid </code></pre> <p>But just as <a href="https://stackoverflow.com/a/3043991/4575793">@unwind said</a>, if you want it to terminate in any case, then just</p> <pre><code>kill $pid </code></pre> <p>Otherwise you will have a race condition, where th... | 387, 3300, 5910 | bash, pid, process | <h1>How to check if a process id (PID) exists</h1>
<p>In a bash script, I want to do the following (in pseudo-code):</p>
<pre><code>if [ a process exists with $PID ]; then
kill $PID
fi
</code></pre>
<p>What's the appropriate expression for the conditional statement?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 932 | bash | # How to check if a process id (PID) exists
In a bash script, I want to do the following (in pseudo-code):
```
if [ a process exists with $PID ]; then
kill $PID
fi
```
What's the appropriate expression for the conditional statement? | To check for the existence of a process, use
```
kill -0 $pid
```
But just as [@unwind said](https://stackoverflow.com/a/3043991/4575793), if you want it to terminate in any case, then just
```
kill $pid
```
Otherwise you will have a race condition, where the process might have disappeared after the first `kill -0`... |
1802127 | How to run a PowerShell script without displaying a window? | 253 | 2009-11-26 07:45:06 | <p>How is it possible to run a <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" rel="noreferrer">PowerShell</a> script without displaying a window or any other sign to the user?</p>
<p>In other words, the script should run quietly in the background without any sign to the user.</p>
<p>Extra credit for an ans... | 771,001 | 15,985 | 2025-10-17 09:49:03 | 1,802,836 | 220 | 2009-11-26 10:22:09 | 75,224 | 2025-08-20 14:55:25 | https://stackoverflow.com/q/1802127 | https://stackoverflow.com/a/1802836 | <p>You can either run it like this (but <strong>this shows a window for a while</strong>):</p>
<pre><code>PowerShell.exe -WindowStyle hidden { your script.. }
</code></pre>
<p>Or you use a helper file I created to avoid the window called PsRun.exe that does exactly that. You can download the source and exe file from <a... | <p>You can either run it like this (but <strong>this shows a window for a while</strong>):</p> <pre><code>PowerShell.exe -WindowStyle hidden { your script.. } </code></pre> <p>Or you use a helper file I created to avoid the window called PsRun.exe that does exactly that. You can download the source and exe file from <a... | 64, 526, 531, 7002, 15545 | batch-file, powershell, scripting, silent, windows | <h1>How to run a PowerShell script without displaying a window?</h1>
<p>How is it possible to run a <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" rel="noreferrer">PowerShell</a> script without displaying a window or any other sign to the user?</p>
<p>In other words, the script should run quietly in the bac... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 933 | bash | # How to run a PowerShell script without displaying a window?
How is it possible to run a [PowerShell](http://en.wikipedia.org/wiki/Windows_PowerShell) script without displaying a window or any other sign to the user?
In other words, the script should run quietly in the background without any sign to the user.
Extra... | You can either run it like this (but **this shows a window for a while**):
```
PowerShell.exe -WindowStyle hidden { your script.. }
```
Or you use a helper file I created to avoid the window called PsRun.exe that does exactly that. You can download the source and exe file from [Run scheduled tasks with WinForm GUI in... |
281372 | Executing Shell Scripts from the OS X Dock? | 188 | 2008-11-11 16:24:50 | <p>How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?</p>
| 135,076 | 5,291 | 2024-11-17 11:54:06 | 281,455 | 220 | 2008-11-11 16:47:04 | 745 | 2012-10-23 12:36:37 | https://stackoverflow.com/q/281372 | https://stackoverflow.com/a/281455 | <p>You could create a Automator workflow with a single step - "Run Shell Script"</p>
<p>Then <code>File > Save As</code>, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.</p>
<p>The benefit to this is i... | <p>You could create a Automator workflow with a single step - "Run Shell Script"</p> <p>Then <code>File > Save As</code>, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.</p> <p>The benefit to this is i... | 369, 390, 5741 | dock, macos, shell | <h1>Executing Shell Scripts from the OS X Dock?</h1>
<p>How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 934 | bash | # Executing Shell Scripts from the OS X Dock?
How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing? | You could create a Automator workflow with a single step - "Run Shell Script"
Then `File > Save As`, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's *really* simple to do, and ... |
7865492 | In Windows 7/10/11 Git Bash, is there a way to explore the directory at the current location? | 86 | 2011-10-23 10:07:18 | <p>I would like to explore the folder in the Windows Explorer from the shell. I know a lot of people have been asking to Cmd here from a folder.</p>
<p>I'd like to do just the opposite and open a new window located at the current directory being browsed.</p>
| 49,238 | 342,840 | 2025-09-30 10:44:53 | 7,865,509 | 220 | 2011-10-23 10:11:21 | 679,340 | 2018-07-25 21:19:06 | https://stackoverflow.com/q/7865492 | https://stackoverflow.com/a/7865509 | <p>To open Windows Explorer at the current folder, just enter:</p>
<pre><code>explorer .
</code></pre>
| <p>To open Windows Explorer at the current folder, just enter:</p> <pre><code>explorer . </code></pre> | 64, 390, 2631, 34595, 61874 | cmd, git-bash, shell, windows, windows-7 | <h1>In Windows 7/10/11 Git Bash, is there a way to explore the directory at the current location?</h1>
<p>I would like to explore the folder in the Windows Explorer from the shell. I know a lot of people have been asking to Cmd here from a folder.</p>
<p>I'd like to do just the opposite and open a new window located a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 935 | bash | # In Windows 7/10/11 Git Bash, is there a way to explore the directory at the current location?
I would like to explore the folder in the Windows Explorer from the shell. I know a lot of people have been asking to Cmd here from a folder.
I'd like to do just the opposite and open a new window located at the current di... | To open Windows Explorer at the current folder, just enter:
```
explorer .
``` |
30078281 | Raise error in a Bash script | 204 | 2015-05-06 13:30:01 | <p>I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?</p>
<p>For example:</p>
<pre><code>if [ condition ]; then
raise error "Test cases failed !!!"
fi
</code></pre>
| 242,258 | 3,078,630 | 2020-10-29 06:01:14 | 30,078,353 | 219 | 2015-05-06 13:33:17 | 4,354,477 | 2015-05-06 13:53:52 | https://stackoverflow.com/q/30078281 | https://stackoverflow.com/a/30078353 | <p>This depends on where you want the error message be stored. </p>
<p>You can do the following:</p>
<pre><code>echo "Error!" > logfile.log
exit 125
</code></pre>
<p>Or the following:</p>
<pre><code>echo "Error!" 1>&2
exit 64
</code></pre>
<p>When you raise an exception you stop the program's execution. ... | <p>This depends on where you want the error message be stored. </p> <p>You can do the following:</p> <pre><code>echo "Error!" > logfile.log exit 125 </code></pre> <p>Or the following:</p> <pre><code>echo "Error!" 1>&2 exit 64 </code></pre> <p>When you raise an exception you stop the program's execution. ... | 58, 379, 387, 390, 531 | bash, error-handling, linux, scripting, shell | <h1>Raise error in a Bash script</h1>
<p>I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?</p>
<p>For example:</p>
<pre><code>if [ condition ]; then
raise error "Test cases failed !!!"
fi
</code></pre>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 936 | bash | # Raise error in a Bash script
I want to raise an error in a Bash script with message "Test cases Failed !!!". How to do this in Bash?
For example:
```
if [ condition ]; then
raise error "Test cases failed !!!"
fi
``` | This depends on where you want the error message be stored.
You can do the following:
```
echo "Error!" > logfile.log
exit 125
```
Or the following:
```
echo "Error!" 1>&2
exit 64
```
When you raise an exception you stop the program's execution.
You can also use something like `exit xxx` where `xxx` is the error ... |
12941083 | Execute and get the output of a shell command in node.js | 197 | 2012-10-17 18:41:11 | <p>In a node.js, I'd like to find a way to obtain the output of a Unix terminal command. Is there any way to do this?</p>
<pre><code>function getCommandOutput(commandString){
// now how can I implement this function?
// getCommandOutput("ls") should print the terminal output of the shell command "ls"
}
</code>... | 236,308 | 975,097 | 2023-11-22 03:44:19 | 12,941,186 | 219 | 2012-10-17 18:47:32 | 91,403 | 2020-11-27 01:02:18 | https://stackoverflow.com/q/12941083 | https://stackoverflow.com/a/12941186 | <p>This is the method I'm using in a project I am currently working on.</p>
<pre><code>var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
</code></pre>
<p>Example of retrieving a git user:</p>
<pre><code>module.expor... | <p>This is the method I'm using in a project I am currently working on.</p> <pre><code>var exec = require('child_process').exec; function execute(command, callback){ exec(command, function(error, stdout, stderr){ callback(stdout); }); }; </code></pre> <p>Example of retrieving a git user:</p> <pre><code>module.expor... | 390, 11225, 46426 | command-line-interface, node.js, shell | <h1>Execute and get the output of a shell command in node.js</h1>
<p>In a node.js, I'd like to find a way to obtain the output of a Unix terminal command. Is there any way to do this?</p>
<pre><code>function getCommandOutput(commandString){
// now how can I implement this function?
// getCommandOutput("ls") sh... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 937 | bash | # Execute and get the output of a shell command in node.js
In a node.js, I'd like to find a way to obtain the output of a Unix terminal command. Is there any way to do this?
```
function getCommandOutput(commandString){
// now how can I implement this function?
// getCommandOutput("ls") should print the termi... | This is the method I'm using in a project I am currently working on.
```
var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
```
Example of retrieving a git user:
```
module.exports.getGitUser = function(callback){... |
905144 | Changing all occurrences in a folder | 160 | 2009-05-25 02:42:57 | <p>I need to do a regex find and replace on all the files in a folder (and its subfolders). What would be the linux shell command to do that?</p>
<p>For example, I want to run this over all the files and overwrite the old file with the new, replaced text.</p>
<pre><code>sed 's/old text/new text/g'
</code></pre>
| 157,269 | 9,021 | 2022-06-29 08:13:39 | 905,161 | 219 | 2009-05-25 02:51:41 | 65,250 | 2009-05-25 03:13:59 | https://stackoverflow.com/q/905144 | https://stackoverflow.com/a/905161 | <p>There is no way to do it using only sed. You'll need to use at least the find utility together:</p>
<pre><code>find . -type f -exec sed -i.bak "s/foo/bar/g" {} \;
</code></pre>
<p>This command will create a <code>.bak</code> file for each changed file.</p>
<p><strong>Notes:</strong> </p>
<ul>
<li>The <code>-i</c... | <p>There is no way to do it using only sed. You'll need to use at least the find utility together:</p> <pre><code>find . -type f -exec sed -i.bak "s/foo/bar/g" {} \; </code></pre> <p>This command will create a <code>.bak</code> file for each changed file.</p> <p><strong>Notes:</strong> </p> <ul> <li>The <code>-i</c... | 18, 58, 390, 5282 | linux, regex, sed, shell | <h1>Changing all occurrences in a folder</h1>
<p>I need to do a regex find and replace on all the files in a folder (and its subfolders). What would be the linux shell command to do that?</p>
<p>For example, I want to run this over all the files and overwrite the old file with the new, replaced text.</p>
<pre><code>s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 938 | bash | # Changing all occurrences in a folder
I need to do a regex find and replace on all the files in a folder (and its subfolders). What would be the linux shell command to do that?
For example, I want to run this over all the files and overwrite the old file with the new, replaced text.
```
sed 's/old text/new text/g'
... | There is no way to do it using only sed. You'll need to use at least the find utility together:
```
find . -type f -exec sed -i.bak "s/foo/bar/g" {} \;
```
This command will create a `.bak` file for each changed file.
**Notes:**
- The `-i` argument for `sed` command is a GNU extension, so, if you are running this c... |
3800202 | Diff output from two programs without temporary files | 150 | 2010-09-26 23:04:58 | <p>Say I have too programs <code>a</code> and <code>b</code> that I can run with <code>./a</code> and <code>./b</code>.</p>
<p>Is it possible to diff their outputs without first writing to temporary files?</p>
| 33,263 | 84,399 | 2024-05-15 18:50:29 | 3,800,207 | 219 | 2010-09-26 23:06:16 | 68,587 | 2024-05-15 18:50:29 | https://stackoverflow.com/q/3800202 | https://stackoverflow.com/a/3800207 | <p>Use <code><(command)</code> to pass one command's output to another program as if it were a file name. Bash pipes the program's output to a pipe and passes a file name like <code>/dev/fd/63</code> to the outer command.</p>
<pre><code>diff <(./a) <(./b)
</code></pre>
<p>Similarly you can use <code>>(comma... | <p>Use <code><(command)</code> to pass one command's output to another program as if it were a file name. Bash pipes the program's output to a pipe and passes a file name like <code>/dev/fd/63</code> to the outer command.</p> <pre><code>diff <(./a) <(./b) </code></pre> <p>Similarly you can use <code>>(comma... | 387, 606, 5310 | bash, diff, file | <h1>Diff output from two programs without temporary files</h1>
<p>Say I have too programs <code>a</code> and <code>b</code> that I can run with <code>./a</code> and <code>./b</code>.</p>
<p>Is it possible to diff their outputs without first writing to temporary files?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 939 | bash | # Diff output from two programs without temporary files
Say I have too programs `a` and `b` that I can run with `./a` and `./b`.
Is it possible to diff their outputs without first writing to temporary files? | Use `<(command)` to pass one command's output to another program as if it were a file name. Bash pipes the program's output to a pipe and passes a file name like `/dev/fd/63` to the outer command.
```
diff <(./a) <(./b)
```
Similarly you can use `>(command)` if you want to pipe something *into* a command.
This is ca... |
44758736 | Redirect stderr to /dev/null for two piped commands: find | grep | 149 | 2017-06-26 11:31:15 | <p>I am using the following command on a Unix server:</p>
<pre><code>find . -type f -name "*.txt" | xargs grep -li 'needle'
</code></pre>
<p>Since <code>grep -R</code> is not available, I have to use this <a href="https://en.wikipedia.org/wiki/Find_(Unix)" rel="noreferrer">find</a>/<a href="https://en.wikipedia.org/w... | 207,907 | 1,820,480 | 2024-05-21 13:44:08 | 44,758,924 | 219 | 2017-06-26 11:41:07 | 171,318 | 2018-11-09 17:47:42 | https://stackoverflow.com/q/44758736 | https://stackoverflow.com/a/44758924 | <p>In order to <a href="https://www.gnu.org/software/bash/manual/html_node/Redirections.html" rel="noreferrer">redirect</a> stderr to /dev/null use:</p>
<pre><code>some_cmd 2>/dev/null
</code></pre>
<p>You don't need <code>xargs</code> here. (And you don't want it! since it performs word splitting)</p>
<p>Use fin... | <p>In order to <a href="https://www.gnu.org/software/bash/manual/html_node/Redirections.html" rel="noreferrer">redirect</a> stderr to /dev/null use:</p> <pre><code>some_cmd 2>/dev/null </code></pre> <p>You don't need <code>xargs</code> here. (And you don't want it! since it performs word splitting)</p> <p>Use fin... | 58, 387, 1271, 5813, 10327 | bash, grep, linux, pipe, sh | <h1>Redirect stderr to /dev/null for two piped commands: find | grep</h1>
<p>I am using the following command on a Unix server:</p>
<pre><code>find . -type f -name "*.txt" | xargs grep -li 'needle'
</code></pre>
<p>Since <code>grep -R</code> is not available, I have to use this <a href="https://en.wikipedia.org/wiki/... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 940 | bash | # Redirect stderr to /dev/null for two piped commands: find | grep
I am using the following command on a Unix server:
```
find . -type f -name "*.txt" | xargs grep -li 'needle'
```
Since `grep -R` is not available, I have to use this [find](https://en.wikipedia.org/wiki/Find_(Unix))/[xargs](https://en.wikipedia.org/... | In order to [redirect](https://www.gnu.org/software/bash/manual/html_node/Redirections.html) stderr to /dev/null use:
```
some_cmd 2>/dev/null
```
You don't need `xargs` here. (And you don't want it! since it performs word splitting)
Use find's exec option:
```
find . -type f -name "*.txt" -exec grep -li needle {} ... |
28501072 | how to check which version of nltk, scikit learn installed? | 144 | 2015-02-13 13:46:40 | <p>In shell script I am checking whether this packages are installed or not, if not installed then install it. So withing shell script:</p>
<pre><code>import nltk
echo nltk.__version__
</code></pre>
<p>but it stops shell script at <code>import</code> line</p>
<p>in linux terminal tried to see in this manner:</p>
<p... | 381,770 | 4,553,951 | 2022-09-17 01:24:40 | 28,501,150 | 219 | 2015-02-13 13:51:06 | 3,005,188 | 2015-02-13 13:51:06 | https://stackoverflow.com/q/28501072 | https://stackoverflow.com/a/28501150 | <p><code>import nltk</code> is Python syntax, and as such won't work in a shell script.</p>
<p>To test the version of <code>nltk</code> and <code>scikit_learn</code>, you can write a <strong>Python script</strong> and run it. Such a script may look like</p>
<pre><code>import nltk
import sklearn
print('The nltk versi... | <p><code>import nltk</code> is Python syntax, and as such won't work in a shell script.</p> <p>To test the version of <code>nltk</code> and <code>scikit_learn</code>, you can write a <strong>Python script</strong> and run it. Such a script may look like</p> <pre><code>import nltk import sklearn print('The nltk versi... | 16, 58, 390, 16712, 81490 | linux, nltk, python, scikit-learn, shell | <h1>how to check which version of nltk, scikit learn installed?</h1>
<p>In shell script I am checking whether this packages are installed or not, if not installed then install it. So withing shell script:</p>
<pre><code>import nltk
echo nltk.__version__
</code></pre>
<p>but it stops shell script at <code>import</code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 941 | bash | # how to check which version of nltk, scikit learn installed?
In shell script I am checking whether this packages are installed or not, if not installed then install it. So withing shell script:
```
import nltk
echo nltk.__version__
```
but it stops shell script at `import` line
in linux terminal tried to see in th... | `import nltk` is Python syntax, and as such won't work in a shell script.
To test the version of `nltk` and `scikit_learn`, you can write a **Python script** and run it. Such a script may look like
```
import nltk
import sklearn
print('The nltk version is {}.'.format(nltk.__version__))
print('The scikit-learn versio... |
14714284 | Powershell: Count items in a folder with PowerShell | 138 | 2013-02-05 18:25:01 | <p>I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (<code>c:\MyFolder</code>). Here's what I've done:</p>
<pre><code>Write-Host ( Get-ChildItem c:\MyFolder ).Count;
</code></pre>
<p>The problem is, that if I have 1 or 0 items, the co... | 306,548 | 967,901 | 2023-01-16 15:50:59 | 14,716,609 | 219 | 2013-02-05 20:46:39 | 2,022,667 | 2023-01-16 15:50:59 | https://stackoverflow.com/q/14714284 | https://stackoverflow.com/a/14716609 | <p>You should use <code>Measure-Object</code> to count things. In this case it would look like:</p>
<pre><code>Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count;
</code></pre>
<p>or if that's too long</p>
<pre><code>Write-Host ( dir c:\MyFolder | measure).Count;
</code></pre>
<p>and in PowerShell 4.0 use ... | <p>You should use <code>Measure-Object</code> to count things. In this case it would look like:</p> <pre><code>Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count; </code></pre> <p>or if that's too long</p> <pre><code>Write-Host ( dir c:\MyFolder | measure).Count; </code></pre> <p>and in PowerShell 4.0 use ... | 24067 | powershell-2.0 | <h1>Powershell: Count items in a folder with PowerShell</h1>
<p>I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (<code>c:\MyFolder</code>). Here's what I've done:</p>
<pre><code>Write-Host ( Get-ChildItem c:\MyFolder ).Count;
</code><... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 942 | bash | # Powershell: Count items in a folder with PowerShell
I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (`c:\MyFolder`). Here's what I've done:
```
Write-Host ( Get-ChildItem c:\MyFolder ).Count;
```
The problem is, that if I have 1 or... | You should use `Measure-Object` to count things. In this case it would look like:
```
Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count;
```
or if that's too long
```
Write-Host ( dir c:\MyFolder | measure).Count;
```
and in PowerShell 4.0 use the `measure` alias instead of `mo`
```
Write-Host (dir c... |
14370133 | Is there a way to create key-value pairs in Bash script? | 125 | 2013-01-17 00:05:05 | <p>I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic:</p>
<pre><code>declare -d dictionary
defaults write "$dictionary" key -string "$value"
</code></pre>
<p>...where <code>$dictionary</code> is a variable, but this is not working.</p>
<p>Is there a way to create ke... | 223,440 | 1,683,948 | 2024-11-07 16:44:28 | 14,371,026 | 219 | 2013-01-17 01:54:08 | 1,961,383 | 2020-03-21 20:47:49 | https://stackoverflow.com/q/14370133 | https://stackoverflow.com/a/14371026 | <p>In bash version 4 associative arrays were introduced.</p>
<pre><code>declare -A arr
arr["key1"]=val1
arr+=( ["key2"]=val2 ["key3"]=val3 )
</code></pre>
<p>The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc.</p>
<pre><code>for ... | <p>In bash version 4 associative arrays were introduced.</p> <pre><code>declare -A arr arr["key1"]=val1 arr+=( ["key2"]=val2 ["key3"]=val3 ) </code></pre> <p>The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc.</p> <pre><code>for ... | 387, 390, 2575 | associative-array, bash, shell | <h1>Is there a way to create key-value pairs in Bash script?</h1>
<p>I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic:</p>
<pre><code>declare -d dictionary
defaults write "$dictionary" key -string "$value"
</code></pre>
<p>...where <code>$dictionary</code> is a vari... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 943 | bash | # Is there a way to create key-value pairs in Bash script?
I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic:
```
declare -d dictionary
defaults write "$dictionary" key -string "$value"
```
...where `$dictionary` is a variable, but this is not working.
Is there a w... | In bash version 4 associative arrays were introduced.
```
declare -A arr
arr["key1"]=val1
arr+=( ["key2"]=val2 ["key3"]=val3 )
```
The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc.
```
for key in ${!arr[@]}; do
echo ${key} ... |
34340575 | ZSH alias with parameter | 223 | 2015-12-17 17:20:24 | <p>I am trying to make an alias with parameter for my simple git add/commit/push.</p>
<p>I've seen that a function could be used as an alias, so I tried but I didn't make it.</p>
<p>Before I had:</p>
<pre><code>alias gitall="git add . ; git commit -m 'update' ; git push"
</code></pre>
<p>But I want to be able... | 165,789 | 4,511,585 | 2022-12-02 23:25:32 | 34,340,688 | 218 | 2015-12-17 17:26:31 | 900,873 | 2022-02-21 03:19:59 | https://stackoverflow.com/q/34340575 | https://stackoverflow.com/a/34340688 | <p>You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the <code>[]</code>.</p>
<pre><code>gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$... | <p>You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the <code>[]</code>.</p> <pre><code>gitall() { git add . if [ "$1" != "" ] # or better, if [ -n "$... | 390, 1448, 3791, 28566 | alias, shell, zsh, zshrc | <h1>ZSH alias with parameter</h1>
<p>I am trying to make an alias with parameter for my simple git add/commit/push.</p>
<p>I've seen that a function could be used as an alias, so I tried but I didn't make it.</p>
<p>Before I had:</p>
<pre><code>alias gitall="git add . ; git commit -m 'update' ; git push"
</co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 944 | bash | # ZSH alias with parameter
I am trying to make an alias with parameter for my simple git add/commit/push.
I've seen that a function could be used as an alias, so I tried but I didn't make it.
Before I had:
```
alias gitall="git add . ; git commit -m 'update' ; git push"
```
But I want to be able to modify my commi... | You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the `[]`.
```
gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$1" ]
then
git commit -m "$1"
else
... |
951336 | How can I debug a Bash script? | 183 | 2009-06-04 15:36:47 | <p>Is there a way to debug a Bash script?</p>
<p>E.g., something that prints a sort of an execution log, like "calling line 1", "calling line 2", etc.</p>
| 207,504 | 74,142 | 2021-10-25 18:21:42 | 951,352 | 218 | 2009-06-04 15:40:55 | 15,168 | 2015-09-02 20:13:48 | https://stackoverflow.com/q/951336 | https://stackoverflow.com/a/951352 | <pre><code>sh -x script [arg1 ...]
bash -x script [arg1 ...]
</code></pre>
<p>These give you a trace of what is being executed. (See also 'Clarification' near the bottom of the answer.)</p>
<p>Sometimes, you need to control the debugging within the script. In that case, as <a href="https://stackoverflow.com/users/1... | <pre><code>sh -x script [arg1 ...] bash -x script [arg1 ...] </code></pre> <p>These give you a trace of what is being executed. (See also 'Clarification' near the bottom of the answer.)</p> <p>Sometimes, you need to control the debugging within the script. In that case, as <a href="https://stackoverflow.com/users/1... | 387, 390 | bash, shell | <h1>How can I debug a Bash script?</h1>
<p>Is there a way to debug a Bash script?</p>
<p>E.g., something that prints a sort of an execution log, like "calling line 1", "calling line 2", etc.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 945 | bash | # How can I debug a Bash script?
Is there a way to debug a Bash script?
E.g., something that prints a sort of an execution log, like "calling line 1", "calling line 2", etc. | ```
sh -x script [arg1 ...]
bash -x script [arg1 ...]
```
These give you a trace of what is being executed. (See also 'Clarification' near the bottom of the answer.)
Sometimes, you need to control the debugging within the script. In that case, as [Cheeto](https://stackoverflow.com/users/110662/cheeto) [reminded](http... |
6239647 | Using PowerShell credentials without being prompted for a password | 170 | 2011-06-04 21:56:47 | <p>I'd like to restart a remote computer that belongs to a domain. I have an administrator account but I don't know how to use it from powershell.</p>
<p>I know that there is a <code>Restart-Computer</code> cmdlet and that I can pass credential but if my domain is for instance <code>mydomain</code>, my username is <co... | 627,252 | 784,301 | 2025-11-22 02:50:02 | 6,240,319 | 218 | 2011-06-05 00:51:20 | 419 | 2017-10-25 11:54:59 | https://stackoverflow.com/q/6239647 | https://stackoverflow.com/a/6240319 | <p>The problem with <code>Get-Credential</code> is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem.</p>
<p>The following article explains how this works:</p>
<blockquote>
<p><a href="http://web.archive.org/web/20... | <p>The problem with <code>Get-Credential</code> is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem.</p> <p>The following article explains how this works:</p> <blockquote> <p><a href="http://web.archive.org/web/20... | 526, 63177 | powershell, powershell-remoting | <h1>Using PowerShell credentials without being prompted for a password</h1>
<p>I'd like to restart a remote computer that belongs to a domain. I have an administrator account but I don't know how to use it from powershell.</p>
<p>I know that there is a <code>Restart-Computer</code> cmdlet and that I can pass credentia... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 946 | bash | # Using PowerShell credentials without being prompted for a password
I'd like to restart a remote computer that belongs to a domain. I have an administrator account but I don't know how to use it from powershell.
I know that there is a `Restart-Computer` cmdlet and that I can pass credential but if my domain is for i... | The problem with `Get-Credential` is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem.
The following article explains how this works:
> [Using PSCredentials without a prompt](http://web.archive.org/web/2016082203084... |
11696944 | Powershell v3 Invoke-WebRequest HTTPS error | 159 | 2012-07-27 23:39:40 | <p>Using Powershell v3's Invoke-WebRequest and Invoke-RestMethod I have succesfully used the POST method to post a json file to a https website.</p>
<p>The command I'm using is</p>
<pre><code> $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
Invoke-WebRequest -Uri h... | 432,772 | 1,399,935 | 2025-06-26 16:10:43 | 15,841,856 | 218 | 2013-04-05 19:20:05 | 392,184 | 2013-04-05 19:20:05 | https://stackoverflow.com/q/11696944 | https://stackoverflow.com/a/15841856 | <p>This work-around worked for me:
<a href="http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors" rel="noreferrer">http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors</a><... | <p>This work-around worked for me: <a href="http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors" rel="noreferrer">http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors</a><... | 1, 364, 526, 642 | .net, https, powershell, rest | <h1>Powershell v3 Invoke-WebRequest HTTPS error</h1>
<p>Using Powershell v3's Invoke-WebRequest and Invoke-RestMethod I have succesfully used the POST method to post a json file to a https website.</p>
<p>The command I'm using is</p>
<pre><code> $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certifi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 947 | bash | # Powershell v3 Invoke-WebRequest HTTPS error
Using Powershell v3's Invoke-WebRequest and Invoke-RestMethod I have succesfully used the POST method to post a json file to a https website.
The command I'm using is
```
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
Invoke... | This work-around worked for me:
<http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors>
Basically, in your PowerShell script:
```
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAl... |
2837785 | PowerShell Script to Find and Replace for all Files with a Specific Extension | 148 | 2010-05-14 21:36:12 | <p>I have several configuration files nested like such:</p>
<pre><code>C:\Projects\Project_1\project1.config
C:\Projects\Project_2\project2.config
</code></pre>
<p>In my configuration I need to do a string replace like such:</p>
<pre><code><add key="Environment" value="Dev"/>
</code></pre>
... | 191,866 | 214,048 | 2025-05-19 15:40:44 | 2,837,891 | 218 | 2010-05-14 21:57:39 | 227,426 | 2015-10-02 08:56:55 | https://stackoverflow.com/q/2837785 | https://stackoverflow.com/a/2837891 | <p>Here a first attempt at the top of my head. </p>
<pre><code>$configFiles = Get-ChildItem . *.config -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "Dev", "Demo" } |
Set-Content $file.PSPath
}
</code></pre>
| <p>Here a first attempt at the top of my head. </p> <pre><code>$configFiles = Get-ChildItem . *.config -rec foreach ($file in $configFiles) { (Get-Content $file.PSPath) | Foreach-Object { $_ -replace "Dev", "Demo" } | Set-Content $file.PSPath } </code></pre> | 526, 531, 2498, 10193 | find, powershell, replace, scripting | <h1>PowerShell Script to Find and Replace for all Files with a Specific Extension</h1>
<p>I have several configuration files nested like such:</p>
<pre><code>C:\Projects\Project_1\project1.config
C:\Projects\Project_2\project2.config
</code></pre>
<p>In my configuration I need to do a string replace like such:</p>
<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 948 | bash | # PowerShell Script to Find and Replace for all Files with a Specific Extension
I have several configuration files nested like such:
```
C:\Projects\Project_1\project1.config
C:\Projects\Project_2\project2.config
```
In my configuration I need to do a string replace like such:
```
<add key="Environment" value="D... | Here a first attempt at the top of my head.
```
$configFiles = Get-ChildItem . *.config -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "Dev", "Demo" } |
Set-Content $file.PSPath
}
``` |
24655436 | How can I find a file/directory that could be anywhere on linux command line? | 144 | 2014-07-09 13:46:50 | <p>Ideally, I would be able to use a program like </p>
<pre><code>find [file or directory name]
</code></pre>
<p>to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder.</p>
<p>I've also tried locate and which, but none find the fil... | 458,059 | 3,779,669 | 2025-05-08 20:40:22 | 24,655,688 | 218 | 2014-07-09 13:57:28 | 2,415,755 | 2014-07-09 13:57:28 | https://stackoverflow.com/q/24655436 | https://stackoverflow.com/a/24655688 | <p>"Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use <code>find -name "filename"</code></p>
<p>If the file in question is not in the current working directory, you can search your entire machine via </p>
<pr... | <p>"Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use <code>find -name "filename"</code></p> <p>If the file in question is not in the current working directory, you can search your entire machine via </p> <pr... | 58, 387, 1964, 10193, 26269 | bash, find, ksh, linux, locate | <h1>How can I find a file/directory that could be anywhere on linux command line?</h1>
<p>Ideally, I would be able to use a program like </p>
<pre><code>find [file or directory name]
</code></pre>
<p>to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory,... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 949 | bash | # How can I find a file/directory that could be anywhere on linux command line?
Ideally, I would be able to use a program like
```
find [file or directory name]
```
to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder.
I've also... | "Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use `find -name "filename"`
If the file in question is not in the current working directory, you can search your entire machine via
```
find / -name "filename"
```... |
2210349 | Test whether string is a valid integer | 143 | 2010-02-05 20:55:27 | <p>I'm trying to do something common enough: Parse user input in a shell script. If the user provided a valid integer, the script does one thing, and if not valid, it does something else. Trouble is, I haven't found an easy (and reasonably elegant) way of doing this - I don't want to have to pick it apart char by char.... | 194,872 | 26,976 | 2020-12-02 21:25:20 | 2,210,386 | 218 | 2010-02-05 21:00:59 | 20,862 | 2016-09-19 15:23:21 | https://stackoverflow.com/q/2210349 | https://stackoverflow.com/a/2210386 | <pre><code>[[ $var =~ ^-?[0-9]+$ ]]
</code></pre>
<ul>
<li>The <code>^</code> indicates the beginning of the input pattern</li>
<li>The <code>-</code> is a literal "-"</li>
<li>The <code>?</code> means "0 or 1 of the preceding (<code>-</code>)"</li>
<li>The <code>+</code> means "1 or more of the preceding (<code>[0-9]... | <pre><code>[[ $var =~ ^-?[0-9]+$ ]] </code></pre> <ul> <li>The <code>^</code> indicates the beginning of the input pattern</li> <li>The <code>-</code> is a literal "-"</li> <li>The <code>?</code> means "0 or 1 of the preceding (<code>-</code>)"</li> <li>The <code>+</code> means "1 or more of the preceding (<code>[0-9]... | 139, 387, 2054, 2773 | bash, if-statement, integer, string | <h1>Test whether string is a valid integer</h1>
<p>I'm trying to do something common enough: Parse user input in a shell script. If the user provided a valid integer, the script does one thing, and if not valid, it does something else. Trouble is, I haven't found an easy (and reasonably elegant) way of doing this - I d... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 950 | bash | # Test whether string is a valid integer
I'm trying to do something common enough: Parse user input in a shell script. If the user provided a valid integer, the script does one thing, and if not valid, it does something else. Trouble is, I haven't found an easy (and reasonably elegant) way of doing this - I don't want... | ```
[[ $var =~ ^-?[0-9]+$ ]]
```
- The `^` indicates the beginning of the input pattern
- The `-` is a literal "-"
- The `?` means "0 or 1 of the preceding (`-`)"
- The `+` means "1 or more of the preceding (`[0-9]`)"
- The `$` indicates the end of the input pattern
So the regex matches an optional `-` (for the case ... |
36495669 | Difference between terms: "option", "argument", and "parameter"? | 130 | 2016-04-08 09:07:09 | <p>What are the differences between these terms: "option", "argument", and "parameter"? In man pages these terms often seem to be used interchangeably.</p>
| 78,090 | 1,881,641 | 2022-09-05 06:17:22 | 36,495,940 | 218 | 2016-04-08 09:19:54 | 211,665 | 2022-05-10 15:46:51 | https://stackoverflow.com/q/36495669 | https://stackoverflow.com/a/36495940 | <p>A <em>command</em> is split into an array of strings named <strong>arguments</strong>. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called <em>positional parameters</em>.</p>
<pre><code>$ ls -la /tmp /var/tmp
arg0 = ls
arg1 =... | <p>A <em>command</em> is split into an array of strings named <strong>arguments</strong>. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called <em>positional parameters</em>.</p> <pre><code>$ ls -la /tmp /var/tmp arg0 = ls arg1 =... | 360, 387, 390, 2313 | arguments, bash, parameters, shell | <h1>Difference between terms: "option", "argument", and "parameter"?</h1>
<p>What are the differences between these terms: "option", "argument", and "parameter"? In man pages these terms often seem to be used interchangeably.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 951 | bash | # Difference between terms: "option", "argument", and "parameter"?
What are the differences between these terms: "option", "argument", and "parameter"? In man pages these terms often seem to be used interchangeably. | A *command* is split into an array of strings named **arguments**. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called *positional parameters*.
```
$ ls -la /tmp /var/tmp
arg0 = ls
arg1 = -la
arg2 = /tmp
arg3 = /var/tmp
```
An... |
2981878 | Checking for environment variables | 113 | 2010-06-05 20:02:10 | <p>I am trying to check the value of an environment variable and depending on the value do certain things and it works fine as long as the variable is set. When it isn't though I get a whole bunch of errors (as BASH is trying to compare the string I specify with an undefined variable, I guess)</p>
<p>I tried implement... | 170,959 | 282,733 | 2022-05-10 10:25:03 | 2,981,883 | 218 | 2010-06-05 20:04:41 | null | 2021-03-10 15:32:11 | https://stackoverflow.com/q/2981878 | https://stackoverflow.com/a/2981883 | <p>Enclose the variable in double-quotes.</p>
<pre><code>if [ "$TESTVAR" == "foo" ]
</code></pre>
<p>if you do that and the variable is empty, the test expands to:</p>
<pre><code>if [ "" == "foo" ]
</code></pre>
<p>whereas if you don't quote it, it expands to:</p>
<pre><code>if [... | <p>Enclose the variable in double-quotes.</p> <pre><code>if [ "$TESTVAR" == "foo" ] </code></pre> <p>if you do that and the variable is empty, the test expands to:</p> <pre><code>if [ "" == "foo" ] </code></pre> <p>whereas if you don't quote it, it expands to:</p> <pre><code>if [... | 58, 387, 390 | bash, linux, shell | <h1>Checking for environment variables</h1>
<p>I am trying to check the value of an environment variable and depending on the value do certain things and it works fine as long as the variable is set. When it isn't though I get a whole bunch of errors (as BASH is trying to compare the string I specify with an undefined ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 952 | bash | # Checking for environment variables
I am trying to check the value of an environment variable and depending on the value do certain things and it works fine as long as the variable is set. When it isn't though I get a whole bunch of errors (as BASH is trying to compare the string I specify with an undefined variable,... | Enclose the variable in double-quotes.
```
if [ "$TESTVAR" == "foo" ]
```
if you do that and the variable is empty, the test expands to:
```
if [ "" == "foo" ]
```
whereas if you don't quote it, it expands to:
```
if [ == "foo" ]
```
which is a syntax error. |
58796490 | tsc.ps1 cannot be loaded because running scripts is disabled on this system | 105 | 2019-11-11 06:27:10 | <p>On PowerShell, I got the error message when executing <code>tsc</code>. This never happened before.</p>
<p>I am not sure should I mingle with the PowerShell security settings to rectify this such as based on this one: <a href="https://stackoverflow.com/questions/4037939/powershell-says-execution-of-scripts-is-disab... | 163,961 | 9,319,588 | 2025-02-27 19:20:21 | 58,801,137 | 218 | 2019-11-11 12:14:19 | 3,735,690 | 2024-04-23 15:31:58 | https://stackoverflow.com/q/58796490 | https://stackoverflow.com/a/58801137 | <p>You can run this command in PowerShell. Make sure to run it as administrator:</p>
<pre><code>Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
</code></pre>
| <p>You can run this command in PowerShell. Make sure to run it as administrator:</p> <pre><code>Set-ExecutionPolicy -ExecutionPolicy RemoteSigned </code></pre> | 526, 84518 | powershell, typescript | <h1>tsc.ps1 cannot be loaded because running scripts is disabled on this system</h1>
<p>On PowerShell, I got the error message when executing <code>tsc</code>. This never happened before.</p>
<p>I am not sure should I mingle with the PowerShell security settings to rectify this such as based on this one: <a href="http... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 953 | bash | # tsc.ps1 cannot be loaded because running scripts is disabled on this system
On PowerShell, I got the error message when executing `tsc`. This never happened before.
I am not sure should I mingle with the PowerShell security settings to rectify this such as based on this one: [PowerShell says "execution of scripts i... | You can run this command in PowerShell. Make sure to run it as administrator:
```
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
``` |
18616581 | How to properly -filter multiple strings in a PowerShell copy script | 90 | 2013-09-04 14:29:29 | <p>I am using the PowerShell script from <a href="https://stackoverflow.com/questions/5432290/how-to-use-powershell-copy-item-and-keep-structure">this answer</a> to do a file copy. The problem arises when I want to include multiple file types using the filter. </p>
<pre><code>Get-ChildItem $originalPath -filter "*.htm... | 153,179 | 1,267,125 | 2021-09-27 04:06:25 | 18,626,464 | 218 | 2013-09-05 01:44:49 | 1,248,365 | 2013-09-05 01:44:49 | https://stackoverflow.com/q/18616581 | https://stackoverflow.com/a/18626464 | <p><strong>-Filter</strong> only accepts a single string. <strong>-Include</strong> accepts multiple values, but qualifies the <strong>-Path</strong> argument. The trick is to append <code>\*</code> to the end of the path, and then use <strong>-Include</strong> to select multiple extensions. BTW, quoting strings is unn... | <p><strong>-Filter</strong> only accepts a single string. <strong>-Include</strong> accepts multiple values, but qualifies the <strong>-Path</strong> argument. The trick is to append <code>\*</code> to the end of the path, and then use <strong>-Include</strong> to select multiple extensions. BTW, quoting strings is unn... | 526, 4330, 10052, 24067 | copy, filter, powershell, powershell-2.0 | <h1>How to properly -filter multiple strings in a PowerShell copy script</h1>
<p>I am using the PowerShell script from <a href="https://stackoverflow.com/questions/5432290/how-to-use-powershell-copy-item-and-keep-structure">this answer</a> to do a file copy. The problem arises when I want to include multiple file types... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 954 | bash | # How to properly -filter multiple strings in a PowerShell copy script
I am using the PowerShell script from [this answer](https://stackoverflow.com/questions/5432290/how-to-use-powershell-copy-item-and-keep-structure) to do a file copy. The problem arises when I want to include multiple file types using the filter.
... | **-Filter** only accepts a single string. **-Include** accepts multiple values, but qualifies the **-Path** argument. The trick is to append `\*` to the end of the path, and then use **-Include** to select multiple extensions. BTW, quoting strings is unnecessary in cmdlet arguments unless they contain spaces or shell s... |
2854655 | Command to escape a string in Bash | 144 | 2010-05-18 04:58:00 | <p>I need a Bash command that will convert a string to something that is escaped. Here's an example:</p>
<pre class="lang-none prettyprint-override"><code>echo "hello\world" | escape | someprog
</code></pre>
<p>Where the escape command makes <code>"hello\world"</code> into <code>"hello\\\world... | 218,914 | 125,380 | 2024-09-10 12:36:27 | 2,856,010 | 217 | 2010-05-18 09:35:45 | 26,428 | 2015-05-27 07:55:56 | https://stackoverflow.com/q/2854655 | https://stackoverflow.com/a/2856010 | <p>In Bash:</p>
<pre><code>printf "%q" "hello\world" | someprog
</code></pre>
<p>for example:</p>
<pre><code>printf "%q" "hello\world"
hello\\world
</code></pre>
<p>This could be used through variables too:</p>
<pre><code>printf -v var "%q\n" "hello\world"
echo "$var"
hello\\world
</code></pre>
| <p>In Bash:</p> <pre><code>printf "%q" "hello\world" | someprog </code></pre> <p>for example:</p> <pre><code>printf "%q" "hello\world" hello\\world </code></pre> <p>This could be used through variables too:</p> <pre><code>printf -v var "%q\n" "hello\world" echo "$var" hello\\world </code></pre> | 387, 4804 | bash, escaping | <h1>Command to escape a string in Bash</h1>
<p>I need a Bash command that will convert a string to something that is escaped. Here's an example:</p>
<pre class="lang-none prettyprint-override"><code>echo "hello\world" | escape | someprog
</code></pre>
<p>Where the escape command makes <code>"hello\world... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 955 | bash | # Command to escape a string in Bash
I need a Bash command that will convert a string to something that is escaped. Here's an example:
```
echo "hello\world" | escape | someprog
```
Where the escape command makes `"hello\world"` into `"hello\\\world"`. Then, *someprog* can use `"hello\\world"` as it expects. Of cour... | In Bash:
```
printf "%q" "hello\world" | someprog
```
for example:
```
printf "%q" "hello\world"
hello\\world
```
This could be used through variables too:
```
printf -v var "%q\n" "hello\world"
echo "$var"
hello\\world
``` |
44222883 | Run a shell script and immediately background it, however keep the ability to inspect its output | 121 | 2017-05-28 00:59:43 | <p>How can I run a shell script and immediately <em>background</em> it, however keep the ability to inspect its output any time by tailing <code>/tmp/output.txt</code>.</p>
<p>It would be nice if I can <em>foreground</em> the process too later.</p>
<hr />
<h3>P.S.</h3>
<p>It would be really cool if you can also show me... | 283,721 | 3,618,156 | 2023-11-05 04:49:03 | 44,222,911 | 217 | 2017-05-28 01:07:20 | 623,316 | 2022-03-13 15:27:21 | https://stackoverflow.com/q/44222883 | https://stackoverflow.com/a/44222911 | <h1>To 'background' a process when you start it</h1>
<p>Simply add an ampersand (<code>&</code>) after the command.</p>
<p>If the program writes to standard out, it will still write to your console / terminal.</p>
<hr />
<h1>To foreground the process</h1>
<p>Simply use the <a href="https://man7.org/linux/man-pages/... | <h1>To 'background' a process when you start it</h1> <p>Simply add an ampersand (<code>&</code>) after the command.</p> <p>If the program writes to standard out, it will still write to your console / terminal.</p> <hr /> <h1>To foreground the process</h1> <p>Simply use the <a href="https://man7.org/linux/man-pages/... | 387, 2510, 5024, 15292, 27223 | background-process, bash, daemon, gnu-screen, job-control | <h1>Run a shell script and immediately background it, however keep the ability to inspect its output</h1>
<p>How can I run a shell script and immediately <em>background</em> it, however keep the ability to inspect its output any time by tailing <code>/tmp/output.txt</code>.</p>
<p>It would be nice if I can <em>foregrou... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 956 | bash | # Run a shell script and immediately background it, however keep the ability to inspect its output
How can I run a shell script and immediately *background* it, however keep the ability to inspect its output any time by tailing `/tmp/output.txt`.
It would be nice if I can *foreground* the process too later.
---
###... | # To 'background' a process when you start it
Simply add an ampersand (`&`) after the command.
If the program writes to standard out, it will still write to your console / terminal.
---
# To foreground the process
Simply use the [`fg`](https://man7.org/linux/man-pages/man1/fg.1p.html) command. You can see a list o... |
8786634 | How to print the number of characters in each line of a text file | 114 | 2012-01-09 10:00:22 | <p>I would like to print the number of characters in each line of a text file using a unix command. I know it is simple with powershell</p>
<pre><code>gc abc.txt | % {$_.length}
</code></pre>
<p>but I need unix command.</p>
| 143,165 | 952,678 | 2019-12-30 12:39:23 | 8,786,674 | 217 | 2012-01-09 10:03:49 | 166,749 | 2019-02-13 17:22:56 | https://stackoverflow.com/q/8786634 | https://stackoverflow.com/a/8786674 | <p>Use Awk.</p>
<pre><code>awk '{ print length }' abc.txt
</code></pre>
| <p>Use Awk.</p> <pre><code>awk '{ print length }' abc.txt </code></pre> | 34, 390, 990, 5282 | awk, sed, shell, unix | <h1>How to print the number of characters in each line of a text file</h1>
<p>I would like to print the number of characters in each line of a text file using a unix command. I know it is simple with powershell</p>
<pre><code>gc abc.txt | % {$_.length}
</code></pre>
<p>but I need unix command.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 957 | bash | # How to print the number of characters in each line of a text file
I would like to print the number of characters in each line of a text file using a unix command. I know it is simple with powershell
```
gc abc.txt | % {$_.length}
```
but I need unix command. | Use Awk.
```
awk '{ print length }' abc.txt
``` |
2414150 | How do I preserve newlines in a quoted string in Bash? | 110 | 2010-03-10 02:37:02 | <p>I'm creating a script to automate the creation of apache virtual hosts. Part of my script goes like this:</p>
<pre><code>MYSTRING="<VirtualHost *:80>
ServerName $NEWVHOST
DocumentRoot /var/www/hosts/$NEWVHOST
...
"
echo $MYSTRING
</code></pre>
<p>However, the line breaks in the script are being ignored. I... | 66,458 | 258,245 | 2017-02-09 02:55:19 | 2,414,157 | 217 | 2010-03-10 02:39:32 | 119,159 | 2010-03-10 02:50:29 | https://stackoverflow.com/q/2414150 | https://stackoverflow.com/a/2414157 | <p>Add quotes to make it work:</p>
<pre><code>echo "$MYSTRING"
</code></pre>
<p>Look at it this way:</p>
<pre><code>MYSTRING="line-1
line-2
line3"
echo $MYSTRING
</code></pre>
<p>this will be executed as:</p>
<pre><code>echo line-1 \
line-2 \
line-3
</code></pre>
<p>i.e. <code>echo</code> with three parameters, ... | <p>Add quotes to make it work:</p> <pre><code>echo "$MYSTRING" </code></pre> <p>Look at it this way:</p> <pre><code>MYSTRING="line-1 line-2 line3" echo $MYSTRING </code></pre> <p>this will be executed as:</p> <pre><code>echo line-1 \ line-2 \ line-3 </code></pre> <p>i.e. <code>echo</code> with three parameters, ... | 387 | bash | <h1>How do I preserve newlines in a quoted string in Bash?</h1>
<p>I'm creating a script to automate the creation of apache virtual hosts. Part of my script goes like this:</p>
<pre><code>MYSTRING="<VirtualHost *:80>
ServerName $NEWVHOST
DocumentRoot /var/www/hosts/$NEWVHOST
...
"
echo $MYSTRING
</code></pre>... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 958 | bash | # How do I preserve newlines in a quoted string in Bash?
I'm creating a script to automate the creation of apache virtual hosts. Part of my script goes like this:
```
MYSTRING="<VirtualHost *:80>
ServerName $NEWVHOST
DocumentRoot /var/www/hosts/$NEWVHOST
...
"
echo $MYSTRING
```
However, the line breaks in the sc... | Add quotes to make it work:
```
echo "$MYSTRING"
```
Look at it this way:
```
MYSTRING="line-1
line-2
line3"
echo $MYSTRING
```
this will be executed as:
```
echo line-1 \
line-2 \
line-3
```
i.e. `echo` with three parameters, printing each parameter with a space in between them.
If you add quotes around `$MYST... |
18042369 | -bash: export: `=': not a valid identifier | 103 | 2013-08-04 11:22:51 | <p>Every time I open my terminal I get the error below:</p>
<pre><code>Last login: Sun Aug 4 17:23:05 on ttys000
-bash: export: `=': not a valid identifier
-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin': not a valid identifier
-bash: export: `=': not a valid identifier
-bash: expor... | 296,780 | 1,548,523 | 2022-12-28 07:28:35 | 18,042,384 | 217 | 2013-08-04 11:24:41 | 635,608 | 2013-08-04 11:24:41 | https://stackoverflow.com/q/18042369 | https://stackoverflow.com/a/18042384 | <p>You cannot put spaces around the <code>=</code> sign when you do:</p>
<pre><code>export foo=bar
</code></pre>
<p>Remove the spaces you have and you should be good to go.</p>
<p>If you type:</p>
<pre><code>export foo = bar
</code></pre>
<p>the shell will interpret that as a request to export three names: <code>f... | <p>You cannot put spaces around the <code>=</code> sign when you do:</p> <pre><code>export foo=bar </code></pre> <p>Remove the spaces you have and you should be good to go.</p> <p>If you type:</p> <pre><code>export foo = bar </code></pre> <p>the shell will interpret that as a request to export three names: <code>f... | 369, 387, 391, 4167 | bash, export, macos, terminal | <h1>-bash: export: `=': not a valid identifier</h1>
<p>Every time I open my terminal I get the error below:</p>
<pre><code>Last login: Sun Aug 4 17:23:05 on ttys000
-bash: export: `=': not a valid identifier
-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin': not a valid identifier
-ba... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 959 | bash | # -bash: export: `=': not a valid identifier
Every time I open my terminal I get the error below:
```
Last login: Sun Aug 4 17:23:05 on ttys000
-bash: export: `=': not a valid identifier
-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin': not a valid identifier
-bash: export: `=': not... | You cannot put spaces around the `=` sign when you do:
```
export foo=bar
```
Remove the spaces you have and you should be good to go.
If you type:
```
export foo = bar
```
the shell will interpret that as a request to export three names: `foo`, `=` and `bar`. `=` isn't a valid variable name, so the command fails.... |
11506124 | How to enter command with password for git pull? | 217 | 2012-07-16 14:10:00 | <p>I want to do this command in one line: </p>
<pre><code>git pull && [my passphrase]
</code></pre>
<p>How to do it?</p>
| 543,050 | 905,486 | 2024-05-01 13:23:20 | 11,506,234 | 216 | 2012-07-16 14:15:54 | 365,237 | 2013-03-12 13:10:58 | https://stackoverflow.com/q/11506124 | https://stackoverflow.com/a/11506234 | <p>This is not exactly what you asked for, but for http(s):</p>
<ul>
<li>you can put the password in .netrc file (_netrc on windows). From there it would be picked up automatically. It would go to your home folder with 600 permissions.</li>
<li>you could also just clone the repo with <code>https://user:pass@domain/rep... | <p>This is not exactly what you asked for, but for http(s):</p> <ul> <li>you can put the password in .netrc file (_netrc on windows). From there it would be picked up automatically. It would go to your home folder with 600 permissions.</li> <li>you could also just clone the repo with <code>https://user:pass@domain/rep... | 119, 387, 1796 | bash, command, git | <h1>How to enter command with password for git pull?</h1>
<p>I want to do this command in one line: </p>
<pre><code>git pull && [my passphrase]
</code></pre>
<p>How 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 | 960 | bash | # How to enter command with password for git pull?
I want to do this command in one line:
```
git pull && [my passphrase]
```
How to do it? | This is not exactly what you asked for, but for http(s):
- you can put the password in .netrc file (_netrc on windows). From there it would be picked up automatically. It would go to your home folder with 600 permissions.
- you could also just clone the repo with `https://user:pass@domain/repo` but that's not really r... |
23356779 | How can I store the "find" command results as an array in Bash | 184 | 2014-04-29 06:07:10 | <p>I am trying to save the result from <code>find</code> as arrays.
Here is my code:</p>
<pre><code>#!/bin/bash
echo "input : "
read input
echo "searching file with this pattern '${input}' under present directory"
array=`find . -name ${input}`
len=${#array[*]}
echo "found : ${len}"
i=0
while [ $i -lt $len ]
do
ec... | 183,289 | 637,242 | 2024-07-28 15:38:20 | 23,357,277 | 216 | 2014-04-29 06:38:42 | 3,030,305 | 2020-05-04 18:54:37 | https://stackoverflow.com/q/23356779 | https://stackoverflow.com/a/23357277 | <h2>Update 2020 for Linux Users:</h2>
<p>If you have an up-to-date version of bash (4.4-alpha or better), as you probably do if you are on Linux, then you should be using <a href="https://stackoverflow.com/a/54561526/3030305">Benjamin W.'s answer</a>.</p>
<p>If you are on Mac OS, which —last I checked— still used bas... | <h2>Update 2020 for Linux Users:</h2> <p>If you have an up-to-date version of bash (4.4-alpha or better), as you probably do if you are on Linux, then you should be using <a href="https://stackoverflow.com/a/54561526/3030305">Benjamin W.'s answer</a>.</p> <p>If you are on Mac OS, which —last I checked— still used bas... | 114, 276, 387, 10193 | arrays, bash, find, variables | <h1>How can I store the "find" command results as an array in Bash</h1>
<p>I am trying to save the result from <code>find</code> as arrays.
Here is my code:</p>
<pre><code>#!/bin/bash
echo "input : "
read input
echo "searching file with this pattern '${input}' under present directory"
array=`find . -name ${input}`
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 961 | bash | # How can I store the "find" command results as an array in Bash
I am trying to save the result from `find` as arrays.
Here is my code:
```
#!/bin/bash
echo "input : "
read input
echo "searching file with this pattern '${input}' under present directory"
array=`find . -name ${input}`
len=${#array[*]}
echo "found : ... | ## Update 2020 for Linux Users:
If you have an up-to-date version of bash (4.4-alpha or better), as you probably do if you are on Linux, then you should be using [Benjamin W.'s answer](https://stackoverflow.com/a/54561526/3030305).
If you are on Mac OS, which —last I checked— still used bash 3.2, or are otherwise usi... |
7727640 | What are the differences among grep, awk & sed? | 157 | 2011-10-11 14:26:57 | <p>I am confused about the differences between <code>grep</code>, <code>awk</code> and <code>sed</code> in terms of their role in Unix/Linux system administration and text processing.</p>
| 159,002 | 288,609 | 2023-01-14 09:45:09 | 7,727,894 | 216 | 2011-10-11 14:44:37 | 889,945 | 2013-05-29 18:29:51 | https://stackoverflow.com/q/7727640 | https://stackoverflow.com/a/7727894 | <p>Short definition:</p>
<p><code>grep</code>: search for specific terms in a file</p>
<pre><code>#usage
$ grep This file.txt
Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"
$ cat file.txt
Every line containing "This"
Every line containing "This"
Ev... | <p>Short definition:</p> <p><code>grep</code>: search for specific terms in a file</p> <pre><code>#usage $ grep This file.txt Every line containing "This" Every line containing "This" Every line containing "This" Every line containing "This" $ cat file.txt Every line containing "This" Every line containing "This" Ev... | 58, 387, 990, 1271, 5282 | awk, bash, grep, linux, sed | <h1>What are the differences among grep, awk & sed?</h1>
<p>I am confused about the differences between <code>grep</code>, <code>awk</code> and <code>sed</code> in terms of their role in Unix/Linux system administration and text processing.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 962 | bash | # What are the differences among grep, awk & sed?
I am confused about the differences between `grep`, `awk` and `sed` in terms of their role in Unix/Linux system administration and text processing. | Short definition:
`grep`: search for specific terms in a file
```
#usage
$ grep This file.txt
Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"
$ cat file.txt
Every line containing "This"
Every line containing "This"
Every line containing "That"
Every... |
2746553 | Read values into a shell variable from a pipe | 271 | 2010-04-30 17:51:55 | <p>I am trying to get bash to process data from stdin that gets piped into. None of these work:</p>
<pre><code>echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=
echo "hello world" | read test; echo test=$test
test=
echo "hello world" | test=`cat`; echo test=$test
test... | 438,484 | 177,931 | 2025-12-03 00:22:47 | 6,779,351 | 215 | 2011-07-21 16:19:55 | 730,123 | 2014-04-28 09:19:02 | https://stackoverflow.com/q/2746553 | https://stackoverflow.com/a/6779351 | <p>Use</p>
<pre><code>IFS= read var << EOF
$(foo)
EOF
</code></pre>
<p>You <em>can</em> trick <code>read</code> into accepting from a pipe like this:</p>
<pre><code>echo "hello world" | { read test; echo test=$test; }
</code></pre>
<p>or even write a function like this:</p>
<pre><code>read_from_pipe() { read... | <p>Use</p> <pre><code>IFS= read var << EOF $(foo) EOF </code></pre> <p>You <em>can</em> trick <code>read</code> into accepting from a pipe like this:</p> <pre><code>echo "hello world" | { read test; echo test=$test; } </code></pre> <p>or even write a function like this:</p> <pre><code>read_from_pipe() { read... | 58, 387, 5813 | bash, linux, pipe | <h1>Read values into a shell variable from a pipe</h1>
<p>I am trying to get bash to process data from stdin that gets piped into. None of these work:</p>
<pre><code>echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=
echo "hello world" | read test; echo test=$test
test=
echo &qu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 963 | bash | # Read values into a shell variable from a pipe
I am trying to get bash to process data from stdin that gets piped into. None of these work:
```
echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=
echo "hello world" | read test; echo test=$test
test=
echo "hello world" | test=`cat`; echo test=$test
t... | Use
```
IFS= read var << EOF
$(foo)
EOF
```
You *can* trick `read` into accepting from a pipe like this:
```
echo "hello world" | { read test; echo test=$test; }
```
or even write a function like this:
```
read_from_pipe() { read "$@" <&0; }
```
But there's no point - your variable assignments may not last! A pip... |
545387 | Linux: compute a single hash for a given folder & contents? | 169 | 2009-02-13 09:51:40 | <p>Surely there must be a way to do this easily! </p>
<p>I've tried the Linux command-line apps such as <code>sha1sum</code> and <code>md5sum</code> but they seem only to be able to compute hashes of individual files and output a list of hash values, one for each file. </p>
<p>I need to generate a single hash for the... | 138,030 | 22,185 | 2025-09-25 17:14:12 | 545,413 | 215 | 2009-02-13 09:59:36 | 34,771 | 2019-02-11 17:48:49 | https://stackoverflow.com/q/545387 | https://stackoverflow.com/a/545413 | <p>One possible way would be:<pre>
sha1sum path/to/folder/* | sha1sum
</pre></p>
<p>If there is a whole directory tree, you're probably better off using find and xargs. One possible command would be</p>
<pre>
find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum
</pre>
<p>And, finally, if you al... | <p>One possible way would be:<pre> sha1sum path/to/folder/* | sha1sum </pre></p> <p>If there is a whole directory tree, you're probably better off using find and xargs. One possible command would be</p> <pre> find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum </pre> <p>And, finally, if you al... | 58, 387, 581 | bash, hash, linux | <h1>Linux: compute a single hash for a given folder & contents?</h1>
<p>Surely there must be a way to do this easily! </p>
<p>I've tried the Linux command-line apps such as <code>sha1sum</code> and <code>md5sum</code> but they seem only to be able to compute hashes of individual files and output a list of hash values,... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 964 | bash | # Linux: compute a single hash for a given folder & contents?
Surely there must be a way to do this easily!
I've tried the Linux command-line apps such as `sha1sum` and `md5sum` but they seem only to be able to compute hashes of individual files and output a list of hash values, one for each file.
I need to generate... | One possible way would be:
```
sha1sum path/to/folder/* | sha1sum
```
If there is a whole directory tree, you're probably better off using find and xargs. One possible command would be
```
find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum
```
And, finally, if you also need to take account o... |
9788492 | powershell - extract file name and extension | 145 | 2012-03-20 14:12:56 | <p>I need to extract file name and extension from e.g. my.file.xlsx. I don't know the name of file or extension and there may be more dots in the name, so I need to search the string from the right and when I find first dot (or last from the left), extract the part on the right side and the part on the left side from t... | 361,563 | 809,973 | 2025-07-31 04:35:39 | 9,788,998 | 215 | 2012-03-20 14:45:53 | 243 | 2012-03-20 14:45:53 | https://stackoverflow.com/q/9788492 | https://stackoverflow.com/a/9788998 | <p>If the file is coming off the disk and as others have stated, use the <code>BaseName</code> and <code>Extension</code> properties:</p>
<pre><code>PS C:\> dir *.xlsx | select BaseName,Extension
BaseName Extension
-------- ---------
StackOverflow.com T... | <p>If the file is coming off the disk and as others have stated, use the <code>BaseName</code> and <code>Extension</code> properties:</p> <pre><code>PS C:\> dir *.xlsx | select BaseName,Extension BaseName Extension -------- --------- StackOverflow.com T... | 1, 526, 24067 | .net, powershell, powershell-2.0 | <h1>powershell - extract file name and extension</h1>
<p>I need to extract file name and extension from e.g. my.file.xlsx. I don't know the name of file or extension and there may be more dots in the name, so I need to search the string from the right and when I find first dot (or last from the left), extract the part ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 965 | bash | # powershell - extract file name and extension
I need to extract file name and extension from e.g. my.file.xlsx. I don't know the name of file or extension and there may be more dots in the name, so I need to search the string from the right and when I find first dot (or last from the left), extract the part on the ri... | If the file is coming off the disk and as others have stated, use the `BaseName` and `Extension` properties:
```
PS C:\> dir *.xlsx | select BaseName,Extension
BaseName Extension
-------- ---------
StackOverflow.com Test Config .xlsx
```
If you ... |
5474732 | How can I add a help method to a shell script? | 171 | 2011-03-29 15:06:50 | <p>How do I check if a <code>-h</code> attribute has been passed into a shell script? I would like to display a help message when a user calls <code>myscript.sh -h</code>.</p>
| 219,904 | 171,296 | 2026-01-09 21:53:50 | 5,476,278 | 214 | 2011-03-29 17:11:17 | 7,552 | 2024-12-19 17:11:22 | https://stackoverflow.com/q/5474732 | https://stackoverflow.com/a/5476278 | <p>here's an example for bash:</p>
<pre><code>usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life, the universe and everything
where:
-h show this help text
-s set the seed value (default: 42)"
seed=42
while getopts ':hs:' option; do
case "$option" ... | <p>here's an example for bash:</p> <pre><code>usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life, the universe and everything where: -h show this help text -s set the seed value (default: 42)" seed=42 while getopts ':hs:' option; do case "$option" ... | 34, 390 | shell, unix | <h1>How can I add a help method to a shell script?</h1>
<p>How do I check if a <code>-h</code> attribute has been passed into a shell script? I would like to display a help message when a user calls <code>myscript.sh -h</code>.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 966 | bash | # How can I add a help method to a shell script?
How do I check if a `-h` attribute has been passed into a shell script? I would like to display a help message when a user calls `myscript.sh -h`. | here's an example for bash:
```
usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life, the universe and everything
where:
-h show this help text
-s set the seed value (default: 42)"
seed=42
while getopts ':hs:' option; do
case "$option" in
h) echo "$usage"
exit
... |
1032023 | sed whole word search and replace | 159 | 2009-06-23 11:37:18 | <p>How do I search and replace whole words using sed?</p>
<p>Doing</p>
<pre><code>sed -i 's/[oldtext]/[newtext]/g' <file>
</code></pre>
<p>will also replace partial matches of <code>[oldtext]</code> which I don't want it to do.</p>
| 155,265 | 16,139 | 2021-09-25 13:58:02 | 1,032,039 | 214 | 2009-06-23 11:41:16 | 51,647 | 2009-06-23 11:54:49 | https://stackoverflow.com/q/1032023 | https://stackoverflow.com/a/1032039 | <p>\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):</p>
<pre><code>$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment
</code></pre>
| <p>\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):</p> <pre><code>$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g" no bar embarassment </code></pre> | 390, 5282 | sed, shell | <h1>sed whole word search and replace</h1>
<p>How do I search and replace whole words using sed?</p>
<p>Doing</p>
<pre><code>sed -i 's/[oldtext]/[newtext]/g' <file>
</code></pre>
<p>will also replace partial matches of <code>[oldtext]</code> which I don't want it to do.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 967 | bash | # sed whole word search and replace
How do I search and replace whole words using sed?
Doing
```
sed -i 's/[oldtext]/[newtext]/g' <file>
```
will also replace partial matches of `[oldtext]` which I don't want it to do. | \b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):
```
$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment
``` |
4411014 | How to get only the first ten bytes of a binary file | 128 | 2010-12-10 16:29:50 | <p>I am writing a bash script that needs to get the header (first 10 bytes) of a file and then in another section get everything except the first 10 bytes. These are binary files and will likely have <code>\0</code>'s and <code>\n</code>'s throughout the first 10 bytes. It seems like most utilities work with ASCII fi... | 124,804 | 125,380 | 2025-12-02 16:14:17 | 4,411,216 | 214 | 2010-12-10 16:47:27 | 333,698 | 2010-12-10 16:47:27 | https://stackoverflow.com/q/4411014 | https://stackoverflow.com/a/4411216 | <p>To get the first 10 bytes, as noted already:</p>
<pre><code>head -c 10
</code></pre>
<p>To get all but the first 10 bytes (at least with GNU <code>tail</code>):</p>
<pre><code>tail -c+11
</code></pre>
| <p>To get the first 10 bytes, as noted already:</p> <pre><code>head -c 10 </code></pre> <p>To get all but the first 10 bytes (at least with GNU <code>tail</code>):</p> <pre><code>tail -c+11 </code></pre> | 387, 1456, 1718, 5004 | bash, binaryfiles, byte, text-processing | <h1>How to get only the first ten bytes of a binary file</h1>
<p>I am writing a bash script that needs to get the header (first 10 bytes) of a file and then in another section get everything except the first 10 bytes. These are binary files and will likely have <code>\0</code>'s and <code>\n</code>'s throughout the fi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 968 | bash | # How to get only the first ten bytes of a binary file
I am writing a bash script that needs to get the header (first 10 bytes) of a file and then in another section get everything except the first 10 bytes. These are binary files and will likely have `\0`'s and `\n`'s throughout the first 10 bytes. It seems like most... | To get the first 10 bytes, as noted already:
```
head -c 10
```
To get all but the first 10 bytes (at least with GNU `tail`):
```
tail -c+11
``` |
9011233 | For files in directory, only echo filename (no path) | 127 | 2012-01-25 22:18:56 | <p>How do I go about echoing only the filename of a file if I iterate a directory with a for loop?</p>
<pre><code>for filename in /home/user/*
do
echo $filename
done;
</code></pre>
<p>will pull the full path with the file name. I just want the file name.</p>
| 265,627 | 781,339 | 2024-10-03 23:24:31 | 9,011,264 | 214 | 2012-01-25 22:20:45 | 207,248 | 2019-07-21 20:07:46 | https://stackoverflow.com/q/9011233 | https://stackoverflow.com/a/9011264 | <p>If you want a native <code>bash</code> solution</p>
<pre><code>for file in /home/user/*; do
echo "${file##*/}"
done
</code></pre>
<p>The above uses <strong>Parameter Expansion</strong> which is native to the shell and does not require a call to an external binary such as <code>basename</code></p>
<p>However, mi... | <p>If you want a native <code>bash</code> solution</p> <pre><code>for file in /home/user/*; do echo "${file##*/}" done </code></pre> <p>The above uses <strong>Parameter Expansion</strong> which is native to the shell and does not require a call to an external binary such as <code>basename</code></p> <p>However, mi... | 387, 390 | bash, shell | <h1>For files in directory, only echo filename (no path)</h1>
<p>How do I go about echoing only the filename of a file if I iterate a directory with a for loop?</p>
<pre><code>for filename in /home/user/*
do
echo $filename
done;
</code></pre>
<p>will pull the full path with the file name. I just want the file name.... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 969 | bash | # For files in directory, only echo filename (no path)
How do I go about echoing only the filename of a file if I iterate a directory with a for loop?
```
for filename in /home/user/*
do
echo $filename
done;
```
will pull the full path with the file name. I just want the file name. | If you want a native `bash` solution
```
for file in /home/user/*; do
echo "${file##*/}"
done
```
The above uses **Parameter Expansion** which is native to the shell and does not require a call to an external binary such as `basename`
However, might I suggest just using `find`
```
find /home/user -type f -printf ... |
16396146 | Using the && operator in an if statement | 110 | 2013-05-06 09:52:10 | <p>I have three variables:</p>
<pre><code>VAR1="file1"
VAR2="file2"
VAR3="file3"
</code></pre>
<p>How to use and (<code>&&</code>) operator in if statement like this:</p>
<pre><code>if [ -f $VAR1 && -f $VAR2 && -f $VAR3 ]
then ...
fi
</code></pre>
<p>When I write this code it gives error.... | 296,249 | 1,940,895 | 2019-03-24 18:38:01 | 16,396,181 | 214 | 2013-05-06 09:53:55 | 1,983,854 | 2013-05-06 09:59:13 | https://stackoverflow.com/q/16396146 | https://stackoverflow.com/a/16396181 | <p>So to make your expression work, changing <code>&&</code> for <code>-a</code> will do the trick.</p>
<p>It is correct like this:</p>
<pre><code> if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ]
then ....
</code></pre>
<p>or like</p>
<pre><code> if [[ -f $VAR1 && -f $VAR2 &&am... | <p>So to make your expression work, changing <code>&&</code> for <code>-a</code> will do the trick.</p> <p>It is correct like this:</p> <pre><code> if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ] then .... </code></pre> <p>or like</p> <pre><code> if [[ -f $VAR1 && -f $VAR2 &&am... | 367, 387, 2773, 7992 | bash, if-statement, operators, syntax | <h1>Using the && operator in an if statement</h1>
<p>I have three variables:</p>
<pre><code>VAR1="file1"
VAR2="file2"
VAR3="file3"
</code></pre>
<p>How to use and (<code>&&</code>) operator in if statement like this:</p>
<pre><code>if [ -f $VAR1 && -f $VAR2 && -f $VAR3 ]
then ...
fi
</code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 970 | bash | # Using the && operator in an if statement
I have three variables:
```
VAR1="file1"
VAR2="file2"
VAR3="file3"
```
How to use and (`&&`) operator in if statement like this:
```
if [ -f $VAR1 && -f $VAR2 && -f $VAR3 ]
then ...
fi
```
When I write this code it gives error. What is the right way? | So to make your expression work, changing `&&` for `-a` will do the trick.
It is correct like this:
```
if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ]
then ....
```
or like
```
if [[ -f $VAR1 && -f $VAR2 && -f $VAR3 ]]
then ....
```
or even
```
if [ -f $VAR1 -a -f $VAR2 -a -f $VAR3 ]
then ....
```
You ... |
36155219 | psql: command not found Mac | 194 | 2016-03-22 13:07:13 | <p>I installed PostgreSQL via the graphical install on <a href="http://www.postgresql.org/download/macosx/" rel="noreferrer">http://www.postgresql.org/download/macosx/</a></p>
<p>I see it in my applications and also have the psql terminal in my applications. I need psql to work in the regular terminal for another bash... | 405,180 | 3,147,424 | 2025-10-15 07:07:22 | 36,156,782 | 213 | 2016-03-22 14:13:27 | 2,836,621 | 2022-12-26 11:01:48 | https://stackoverflow.com/q/36155219 | https://stackoverflow.com/a/36156782 | <p>You have got the PATH slightly wrong. You need the PATH to <em>"the containing directory"</em>, not the actual executable itself.</p>
<p>Your PATH should be set like this:</p>
<pre><code>export PATH=/Library/PostgreSQL/9.5/bin:$PATH
</code></pre>
<p>without the extra <code>sql</code> part in it. Also, you ... | <p>You have got the PATH slightly wrong. You need the PATH to <em>"the containing directory"</em>, not the actual executable itself.</p> <p>Your PATH should be set like this:</p> <pre><code>export PATH=/Library/PostgreSQL/9.5/bin:$PATH </code></pre> <p>without the extra <code>sql</code> part in it. Also, you ... | 256, 369, 387, 82549 | bash, macos, postgresql, psql | <h1>psql: command not found Mac</h1>
<p>I installed PostgreSQL via the graphical install on <a href="http://www.postgresql.org/download/macosx/" rel="noreferrer">http://www.postgresql.org/download/macosx/</a></p>
<p>I see it in my applications and also have the psql terminal in my applications. I need psql to work in ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 971 | bash | # psql: command not found Mac
I installed PostgreSQL via the graphical install on <http://www.postgresql.org/download/macosx/>
I see it in my applications and also have the psql terminal in my applications. I need psql to work in the regular terminal for another bash script I'm running for an app.
For some reason, w... | You have got the PATH slightly wrong. You need the PATH to *"the containing directory"*, not the actual executable itself.
Your PATH should be set like this:
```
export PATH=/Library/PostgreSQL/9.5/bin:$PATH
```
without the extra `sql` part in it. Also, you must remove the spaces around the equals sign. |
8055694 | How to execute a MySQL command from a shell script? | 178 | 2011-11-08 19:00:32 | <p>How can I execute an SQL command through a shell script so that I can make it automated?</p>
<p>I want to restore data I have collected in a SQL file using a shell script. I want to connect to a server and restore data. The command works when executed separately via SSH command line.</p>
<p>This is the command I u... | 433,548 | 965,713 | 2024-04-20 21:35:10 | 8,055,745 | 213 | 2011-11-08 19:06:35 | 20,860 | 2024-03-25 06:04:14 | https://stackoverflow.com/q/8055694 | https://stackoverflow.com/a/8055745 | <p>You need to use the <code>-p</code> flag to send a password. And it's tricky because you must have no space between <code>-p</code> and the password.</p>
<pre><code>$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"
</code></pre>
<p>... | <p>You need to use the <code>-p</code> flag to send a password. And it's tricky because you must have no space between <code>-p</code> and the password.</p> <pre><code>$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql" </code></pre> <p>... | 21, 386, 390 | mysql, shell, ssh | <h1>How to execute a MySQL command from a shell script?</h1>
<p>How can I execute an SQL command through a shell script so that I can make it automated?</p>
<p>I want to restore data I have collected in a SQL file using a shell script. I want to connect to a server and restore data. The command works when executed sep... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 972 | bash | # How to execute a MySQL command from a shell script?
How can I execute an SQL command through a shell script so that I can make it automated?
I want to restore data I have collected in a SQL file using a shell script. I want to connect to a server and restore data. The command works when executed separately via SSH ... | You need to use the `-p` flag to send a password. And it's tricky because you must have no space between `-p` and the password.
```
$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"
```
If you use a space after `-p` it makes the `mysql` client prompt you interactively for the password, ... |
25288194 | Don't display pushd/popd stack across several bash scripts (quiet pushd/popd) | 154 | 2014-08-13 13:59:40 | <p>Each time I use pushd or popd, it print the stack to standard output. How not to do so?</p>
<p>I don't want to do <code>pushd > /dev/null</code> each time because I have a lot of scripts calling each other.</p>
<p>Maybe a nice override will do it, but I'll need to override these builtins only in my scripts, and... | 82,027 | 3,307,852 | 2025-01-27 13:11:49 | 25,288,289 | 213 | 2014-08-13 14:04:19 | 1,126,841 | 2014-08-13 14:04:19 | https://stackoverflow.com/q/25288194 | https://stackoverflow.com/a/25288289 | <p>You could add</p>
<pre><code>pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
</code></pre>
<p>to the top of each script. This is probably the minimum amount of work it will take to solve your problem.</p>
| <p>You could add</p> <pre><code>pushd () { command pushd "$@" > /dev/null } popd () { command popd "$@" > /dev/null } </code></pre> <p>to the top of each script. This is probably the minimum amount of work it will take to solve your problem.</p> | 387, 14334, 84917 | bash, built-in, output | <h1>Don't display pushd/popd stack across several bash scripts (quiet pushd/popd)</h1>
<p>Each time I use pushd or popd, it print the stack to standard output. How not to do so?</p>
<p>I don't want to do <code>pushd > /dev/null</code> each time because I have a lot of scripts calling each other.</p>
<p>Maybe a nic... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 973 | bash | # Don't display pushd/popd stack across several bash scripts (quiet pushd/popd)
Each time I use pushd or popd, it print the stack to standard output. How not to do so?
I don't want to do `pushd > /dev/null` each time because I have a lot of scripts calling each other.
Maybe a nice override will do it, but I'll need ... | You could add
```
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
```
to the top of each script. This is probably the minimum amount of work it will take to solve your problem. |
14155669 | Call Python script from bash with argument | 149 | 2013-01-04 10:48:28 | <p>I know that I can run a python script from my bash script using the following:</p>
<pre><code>python python_script.py
</code></pre>
<p>But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?</p>
<p>Basically bash will work out a filename and then python... | 336,984 | 1,738,522 | 2022-03-16 13:09:05 | 32,750,302 | 213 | 2015-09-23 22:07:07 | 2,433,774 | 2022-03-16 13:09:05 | https://stackoverflow.com/q/14155669 | https://stackoverflow.com/a/32750302 | <p>To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance</p>
<pre><code>> python python_script.py var1 var2
</code></pre>
<p>To access these variables within python you will need</p>
<pre><code>import sys
print(sys.argv[0]) # prints python_script... | <p>To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance</p> <pre><code>> python python_script.py var1 var2 </code></pre> <p>To access these variables within python you will need</p> <pre><code>import sys print(sys.argv[0]) # prints python_script... | 16, 58, 387, 390, 1012 | bash, debian, linux, python, shell | <h1>Call Python script from bash with argument</h1>
<p>I know that I can run a python script from my bash script using the following:</p>
<pre><code>python python_script.py
</code></pre>
<p>But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?</p>
<p>Bas... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 974 | bash | # Call Python script from bash with argument
I know that I can run a python script from my bash script using the following:
```
python python_script.py
```
But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?
Basically bash will work out a filename and... | To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance
```
> python python_script.py var1 var2
```
To access these variables within python you will need
```
import sys
print(sys.argv[0]) # prints python_script.py
print(sys.argv[1]) # prints var1
p... |
13767252 | How to split path by last slash? | 111 | 2012-12-07 16:40:04 | <p>I have a file (say called <code>list.txt</code>) that contains relative paths to files, one path per line, i.e. something like this:</p>
<pre><code>foo/bar/file1
foo/bar/baz/file2
goo/file3
</code></pre>
<p>I need to write a bash script that processes one path at a time, splits it at the last slash and then launches... | 126,325 | 1,054,435 | 2022-12-13 08:54:30 | 13,767,314 | 213 | 2012-12-07 16:43:33 | 300,886 | 2021-07-13 09:54:04 | https://stackoverflow.com/q/13767252 | https://stackoverflow.com/a/13767314 | <p>Use <code>basename</code> and <code>dirname</code>, that's all you need.</p>
<pre><code>part1=$(dirname "$p")
part2=$(basename "$p")
</code></pre>
| <p>Use <code>basename</code> and <code>dirname</code>, that's all you need.</p> <pre><code>part1=$(dirname "$p") part2=$(basename "$p") </code></pre> | 387, 2193 | bash, split | <h1>How to split path by last slash?</h1>
<p>I have a file (say called <code>list.txt</code>) that contains relative paths to files, one path per line, i.e. something like this:</p>
<pre><code>foo/bar/file1
foo/bar/baz/file2
goo/file3
</code></pre>
<p>I need to write a bash script that processes one path at a time, spl... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 975 | bash | # How to split path by last slash?
I have a file (say called `list.txt`) that contains relative paths to files, one path per line, i.e. something like this:
```
foo/bar/file1
foo/bar/baz/file2
goo/file3
```
I need to write a bash script that processes one path at a time, splits it at the last slash and then launches... | Use `basename` and `dirname`, that's all you need.
```
part1=$(dirname "$p")
part2=$(basename "$p")
``` |
34051747 | Get Environment Variable from Docker Container | 263 | 2015-12-02 19:51:30 | <p>What's the simplest way to get an environment variable from a docker container <em>that has not been declared in the Dockerfile</em>?</p>
<p>For instance, an environment variable that has been set through some <code>docker exec container /bin/bash</code> session?</p>
<p>I can do <code>docker exec container env | g... | 442,687 | 4,463,660 | 2022-07-20 17:16:37 | 34,052,766 | 212 | 2015-12-02 20:54:04 | 744,178 | 2020-04-27 16:03:00 | https://stackoverflow.com/q/34051747 | https://stackoverflow.com/a/34052766 | <p>The proper way to run <code>echo "$ENV_VAR"</code> inside the container so that the variable substitution happens in the container is:</p>
<pre><code>docker exec <container_id> bash -c 'echo "$ENV_VAR"'
</code></pre>
| <p>The proper way to run <code>echo "$ENV_VAR"</code> inside the container so that the variable substitution happens in the container is:</p> <pre><code>docker exec <container_id> bash -c 'echo "$ENV_VAR"' </code></pre> | 387, 9013, 90304 | bash, docker, environment-variables | <h1>Get Environment Variable from Docker Container</h1>
<p>What's the simplest way to get an environment variable from a docker container <em>that has not been declared in the Dockerfile</em>?</p>
<p>For instance, an environment variable that has been set through some <code>docker exec container /bin/bash</code> sessi... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 976 | bash | # Get Environment Variable from Docker Container
What's the simplest way to get an environment variable from a docker container *that has not been declared in the Dockerfile*?
For instance, an environment variable that has been set through some `docker exec container /bin/bash` session?
I can do `docker exec contain... | The proper way to run `echo "$ENV_VAR"` inside the container so that the variable substitution happens in the container is:
```
docker exec <container_id> bash -c 'echo "$ENV_VAR"'
``` |
9217185 | How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files? | 203 | 2012-02-09 19:10:40 | <p>How do I use grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files?</p>
| 395,732 | 745,835 | 2018-07-27 08:58:51 | 9,217,351 | 212 | 2012-02-09 19:24:03 | 745,835 | 2017-08-10 16:35:52 | https://stackoverflow.com/q/9217185 | https://stackoverflow.com/a/9217351 | <pre><code>grep -r --include=*.{cc,h} "hello" .
</code></pre>
<p>This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this <code>.</code> (current) directory </p>
<p><a href="https://stackoverflow.com/questions/221921/grep-exclude-include-syntax-do-not-grep-thro... | <pre><code>grep -r --include=*.{cc,h} "hello" . </code></pre> <p>This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this <code>.</code> (current) directory </p> <p><a href="https://stackoverflow.com/questions/221921/grep-exclude-include-syntax-do-not-grep-thro... | 34, 58, 387, 1271 | bash, grep, linux, unix | <h1>How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files?</h1>
<p>How do I use grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 977 | bash | # How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files?
How do I use grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files? | ```
grep -r --include=*.{cc,h} "hello" .
```
This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this `.` (current) directory
[From another stackoverflow question](https://stackoverflow.com/questions/221921/grep-exclude-include-syntax-do-not-grep-through-certai... |
2129123 | Rearrange columns using cut | 184 | 2010-01-24 22:17:40 | <p>I am having a file in the following format</p>
<pre>
Column1 Column2
str1 1
str2 2
str3 3
</pre>
<p>I want the columns to be rearranged. I tried below command</p>
<blockquote>
<p>cut -f2,1 file.txt</p>
</blockquote>
<p>The command doesn't reorder the columns. Any idea why its not working?</p>
| 163,575 | 207,335 | 2024-09-25 11:51:35 | 2,129,133 | 212 | 2010-01-24 22:21:35 | 20,862 | 2024-09-25 11:51:35 | https://stackoverflow.com/q/2129123 | https://stackoverflow.com/a/2129133 | <p>For the <code>cut(1)</code> man page:</p>
<blockquote>
<p>Use one, and only one of -b, -c or -f. Each LIST is made up of
one
range, or many ranges separated by commas. Selected input is written
in the same order that it is read, and is written exactly once.</p>
</blockquote>
<p>It reaches field 1 first, so t... | <p>For the <code>cut(1)</code> man page:</p> <blockquote> <p>Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once.</p> </blockquote> <p>It reaches field 1 first, so t... | 73, 390, 4257 | csv, cut, shell | <h1>Rearrange columns using cut</h1>
<p>I am having a file in the following format</p>
<pre>
Column1 Column2
str1 1
str2 2
str3 3
</pre>
<p>I want the columns to be rearranged. I tried below command</p>
<blockquote>
<p>cut -f2,1 file.txt</p>
</blockquote>
<p>The command doesn't reorder the columns.... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 978 | bash | # Rearrange columns using cut
I am having a file in the following format
```
Column1 Column2
str1 1
str2 2
str3 3
```
I want the columns to be rearranged. I tried below command
> cut -f2,1 file.txt
The command doesn't reorder the columns. Any idea why its not working? | For the `cut(1)` man page:
> Use one, and only one of -b, -c or -f. Each LIST is made up of
> one
> range, or many ranges separated by commas. Selected input is written
> in the same order that it is read, and is written exactly once.
It reaches field 1 first, so that is printed, followed by field 2.
Use `awk` inste... |
10516384 | How to delete multiple files at once in Bash on Linux? | 144 | 2012-05-09 12:44:32 | <p>I have this list of files on a Linux server:</p>
<pre><code>abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
abc.log.2012-04-04
abc.log.2012-04-05
abc.log.2012-04-09
abc.log.2012-04-10
</code></pre>
<p>I've been deleting selected log files one by one... | 383,097 | 1,253,847 | 2024-04-13 01:55:43 | 10,516,446 | 212 | 2012-05-09 12:46:57 | 28,169 | 2012-05-09 12:53:44 | https://stackoverflow.com/q/10516384 | https://stackoverflow.com/a/10516446 | <p>Bash supports all sorts of wildcards and expansions.</p>
<p>Your exact case would be handled by <a href="http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion" rel="noreferrer">brace expansion</a>, like so:</p>
<pre><code>$ rm -rf abc.log.2012-03-{14,27,28}
</code></pre>
<p>The above would <em>expa... | <p>Bash supports all sorts of wildcards and expansions.</p> <p>Your exact case would be handled by <a href="http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion" rel="noreferrer">brace expansion</a>, like so:</p> <pre><code>$ rm -rf abc.log.2012-03-{14,27,28} </code></pre> <p>The above would <em>expa... | 58, 387, 5610 | bash, linux, rm | <h1>How to delete multiple files at once in Bash on Linux?</h1>
<p>I have this list of files on a Linux server:</p>
<pre><code>abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
abc.log.2012-04-04
abc.log.2012-04-05
abc.log.2012-04-09
abc.log.2012-04-10
</... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 979 | bash | # How to delete multiple files at once in Bash on Linux?
I have this list of files on a Linux server:
```
abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
abc.log.2012-04-04
abc.log.2012-04-05
abc.log.2012-04-09
abc.log.2012-04-10
```
I've been deletin... | Bash supports all sorts of wildcards and expansions.
Your exact case would be handled by [brace expansion](http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion), like so:
```
$ rm -rf abc.log.2012-03-{14,27,28}
```
The above would *expand to* a single command with all three arguments, and be equivale... |
9701840 | Create a shortcut (.lnk file) using PowerShell | 143 | 2012-03-14 12:19:16 | <p>I want to create a shortcut with PowerShell for this executable: </p>
<pre><code>C:\Program Files (x86)\ColorPix\ColorPix.exe
</code></pre>
<p>How can this be done?</p>
| 309,218 | 1,202,236 | 2024-08-25 01:01:15 | 9,701,907 | 212 | 2012-03-14 12:23:29 | 520,612 | 2024-08-25 01:01:15 | https://stackoverflow.com/q/9701840 | https://stackoverflow.com/a/9701907 | <p>I don't know any native commandlet in PowerShell, but you can use a COM object instead:</p>
<pre><code>$WshShell = New-Object -COMObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "%SystemDrive%\Program Files (x86)\ColorPix\ColorPix.exe"... | <p>I don't know any native commandlet in PowerShell, but you can use a COM object instead:</p> <pre><code>$WshShell = New-Object -COMObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk") $Shortcut.TargetPath = "%SystemDrive%\Program Files (x86)\ColorPix\ColorPix.exe"... | 526, 1231, 7201, 46704 | command-line, powershell, shortcut, shortcut-file | <h1>Create a shortcut (.lnk file) using PowerShell</h1>
<p>I want to create a shortcut with PowerShell for this executable: </p>
<pre><code>C:\Program Files (x86)\ColorPix\ColorPix.exe
</code></pre>
<p>How can this be done?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 980 | bash | # Create a shortcut (.lnk file) using PowerShell
I want to create a shortcut with PowerShell for this executable:
```
C:\Program Files (x86)\ColorPix\ColorPix.exe
```
How can this be done? | I don't know any native commandlet in PowerShell, but you can use a COM object instead:
```
$WshShell = New-Object -COMObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "%SystemDrive%\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()
```
You can c... |
6074362 | How to check syslog in Bash on Linux? | 142 | 2011-05-20 15:45:21 | <p>In C we log this way:</p>
<pre><code>syslog( LOG_INFO, "proxying %s", url );
</code></pre>
<p>In Linux how can we check the log?</p>
| 346,868 | 727,662 | 2024-12-23 05:40:08 | 6,074,429 | 212 | 2011-05-20 15:50:32 | 367,273 | 2011-05-20 15:50:32 | https://stackoverflow.com/q/6074362 | https://stackoverflow.com/a/6074429 | <p>How about <code>less /var/log/syslog</code>?</p>
| <p>How about <code>less /var/log/syslog</code>?</p> | 58, 387, 5304 | bash, linux, syslog | <h1>How to check syslog in Bash on Linux?</h1>
<p>In C we log this way:</p>
<pre><code>syslog( LOG_INFO, "proxying %s", url );
</code></pre>
<p>In Linux how can we check the log?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 981 | bash | # How to check syslog in Bash on Linux?
In C we log this way:
```
syslog( LOG_INFO, "proxying %s", url );
```
In Linux how can we check the log? | How about `less /var/log/syslog`? |
31653271 | How to call cURL without using server-side cache? | 130 | 2015-07-27 12:35:44 | <p>Is there a way to tell cURL command not to use server's side cache?
e.g; I have this curl command:</p>
<p><code>curl -v www.example.com</code></p>
<p>How can I ask curl to send a fresh request to not use the cache?</p>
<p><strong>Note</strong>: I am looking for an executable command in the terminal.</p>
| 164,607 | 277,740 | 2022-10-07 10:27:44 | 36,043,573 | 212 | 2016-03-16 18:04:56 | 6,073,195 | 2016-03-16 18:04:56 | https://stackoverflow.com/q/31653271 | https://stackoverflow.com/a/36043573 | <p>I know this is an older question, but I wanted to post an answer for users with the same question:</p>
<pre><code>curl -H 'Cache-Control: no-cache' http://www.example.com
</code></pre>
<p>This curl command servers in its header request to return non-cached data from the web server.</p>
| <p>I know this is an older question, but I wanted to post an answer for users with the same question:</p> <pre><code>curl -H 'Cache-Control: no-cache' http://www.example.com </code></pre> <p>This curl command servers in its header request to return non-cached data from the web server.</p> | 58, 387, 1249, 1554, 1763 | bash, caching, curl, linux, memcached | <h1>How to call cURL without using server-side cache?</h1>
<p>Is there a way to tell cURL command not to use server's side cache?
e.g; I have this curl command:</p>
<p><code>curl -v www.example.com</code></p>
<p>How can I ask curl to send a fresh request to not use the cache?</p>
<p><strong>Note</strong>: I am looking ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 982 | bash | # How to call cURL without using server-side cache?
Is there a way to tell cURL command not to use server's side cache?
e.g; I have this curl command:
`curl -v www.example.com`
How can I ask curl to send a fresh request to not use the cache?
**Note**: I am looking for an executable command in the terminal. | I know this is an older question, but I wanted to post an answer for users with the same question:
```
curl -H 'Cache-Control: no-cache' http://www.example.com
```
This curl command servers in its header request to return non-cached data from the web server. |
4200800 | In bash, how do I bind a function key to a command? | 127 | 2010-11-17 01:48:31 | <p>Example: I want to bind the <kbd>F12</kbd> key to the command <code>echo "foobar"</code> such that every time I hit <kbd>F12</kbd> the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?</p>
| 111,243 | 207,248 | 2023-02-11 13:16:27 | 4,201,274 | 212 | 2010-11-17 03:42:08 | 26,428 | 2016-02-16 19:43:33 | https://stackoverflow.com/q/4200800 | https://stackoverflow.com/a/4201274 | <p>You can determine the character sequence emitted by a key by pressing <kbd>Ctrl</kbd>-<kbd>v</kbd> at the command line, then pressing the key you're interested in. On my system for <kbd>F12</kbd>, I get <code>^[[24~</code>. The <code>^[</code> represents <kbd>Esc</kbd>. Different types of terminals or terminal emula... | <p>You can determine the character sequence emitted by a key by pressing <kbd>Ctrl</kbd>-<kbd>v</kbd> at the command line, then pressing the key you're interested in. On my system for <kbd>F12</kbd>, I get <code>^[[24~</code>. The <code>^[</code> represents <kbd>Esc</kbd>. Different types of terminals or terminal emula... | 387, 390, 1137 | bash, binding, shell | <h1>In bash, how do I bind a function key to a command?</h1>
<p>Example: I want to bind the <kbd>F12</kbd> key to the command <code>echo "foobar"</code> such that every time I hit <kbd>F12</kbd> the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How do... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 983 | bash | # In bash, how do I bind a function key to a command?
Example: I want to bind the `F12` key to the command `echo "foobar"` such that every time I hit `F12` the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this? | You can determine the character sequence emitted by a key by pressing `Ctrl`-`v` at the command line, then pressing the key you're interested in. On my system for `F12`, I get `^[[24~`. The `^[` represents `Esc`. Different types of terminals or terminal emulators can emit different codes for the same key.
At a Bash pr... |
1629908 | Split output of command by columns using Bash? | 124 | 2009-10-27 10:26:44 | <p>I want to do this: </p>
<ol>
<li>run a command</li>
<li>capture the output</li>
<li>select a line</li>
<li>select a column of that line</li>
</ol>
<p>Just as an example, let's say I want to get the command name from a <code>$PID</code> (please note this is just an example, I'm not suggesting this is the easiest wa... | 240,413 | 63,051 | 2025-03-24 19:10:35 | 1,629,950 | 212 | 2009-10-27 10:34:49 | 28,169 | 2009-10-27 10:34:49 | https://stackoverflow.com/q/1629908 | https://stackoverflow.com/a/1629950 | <p>One easy way is to add a pass of <code><a href="http://linux.die.net/man/1/tr" rel="noreferrer">tr</a></code> to squeeze any repeated field separators out:</p>
<pre><code>$ ps | egrep 11383 | tr -s ' ' | cut -d ' ' -f 4
</code></pre>
| <p>One easy way is to add a pass of <code><a href="http://linux.die.net/man/1/tr" rel="noreferrer">tr</a></code> to squeeze any repeated field separators out:</p> <pre><code>$ ps | egrep 11383 | tr -s ' ' | cut -d ' ' -f 4 </code></pre> | 58, 387, 5813 | bash, linux, pipe | <h1>Split output of command by columns using Bash?</h1>
<p>I want to do this: </p>
<ol>
<li>run a command</li>
<li>capture the output</li>
<li>select a line</li>
<li>select a column of that line</li>
</ol>
<p>Just as an example, let's say I want to get the command name from a <code>$PID</code> (please note this is ju... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 984 | bash | # Split output of command by columns using Bash?
I want to do this:
1. run a command
2. capture the output
3. select a line
4. select a column of that line
Just as an example, let's say I want to get the command name from a `$PID` (please note this is just an example, I'm not suggesting this is the easiest way to ge... | One easy way is to add a pass of `tr` to squeeze any repeated field separators out:
```
$ ps | egrep 11383 | tr -s ' ' | cut -d ' ' -f 4
``` |
874452 | Change the current directory from a Bash script | 252 | 2009-05-17 11:58:01 | <p>Is it possible to change current directory from a script?</p>
<p>I want to create a utility for directory navigation in Bash. I have created a test script that looks like the following:</p>
<pre><code>#!/bin/bash
cd /home/artemb
</code></pre>
<p>When I execute the script from the Bash shell the current directory ... | 393,936 | 89,496 | 2021-08-17 16:44:50 | 874,542 | 211 | 2009-05-17 12:46:45 | 95,853 | 2009-05-17 12:46:45 | https://stackoverflow.com/q/874452 | https://stackoverflow.com/a/874542 | <p>You need to convert your script to a shell function:</p>
<pre><code>#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
# . ~/bin/myprog.sh
#
function myprog() {
A=$1
B=$2
echo "aaa ${A} bbb ${B} ccc"
cd /proc
}
</code></pre>
... | <p>You need to convert your script to a shell function:</p> <pre><code>#!/bin/bash # # this script should not be run directly, # instead you need to source it from your .bashrc, # by adding this line: # . ~/bin/myprog.sh # function myprog() { A=$1 B=$2 echo "aaa ${A} bbb ${B} ccc" cd /proc } </code></pre> ... | 387, 421 | bash, environment | <h1>Change the current directory from a Bash script</h1>
<p>Is it possible to change current directory from a script?</p>
<p>I want to create a utility for directory navigation in Bash. I have created a test script that looks like the following:</p>
<pre><code>#!/bin/bash
cd /home/artemb
</code></pre>
<p>When I exec... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 985 | bash | # Change the current directory from a Bash script
Is it possible to change current directory from a script?
I want to create a utility for directory navigation in Bash. I have created a test script that looks like the following:
```
#!/bin/bash
cd /home/artemb
```
When I execute the script from the Bash shell the c... | You need to convert your script to a shell function:
```
#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
# . ~/bin/myprog.sh
#
function myprog() {
A=$1
B=$2
echo "aaa ${A} bbb ${B} ccc"
cd /proc
}
```
The reason is that each ... |
12996397 | Command not found when using sudo | 235 | 2012-10-21 09:01:16 | <p>I have a script called <code>foo.sh</code> in my home folder. </p>
<p>When I navigate to this folder, and enter <code>./foo.sh</code>, I get </p>
<p><code>-bash: ./foo.sh: Permission denied</code>. </p>
<p>When I use <code>sudo ./foo.sh</code>, I get </p>
<p><code>sudo: foo.sh: command not found</code>.</p>
<p>... | 339,706 | 598,639 | 2022-12-23 14:46:30 | 12,998,638 | 211 | 2012-10-21 14:17:13 | 411,902 | 2013-12-07 01:57:01 | https://stackoverflow.com/q/12996397 | https://stackoverflow.com/a/12998638 | <p><strong>Permission denied</strong></p>
<p>In order to run a script the file must have <strong>an executable permission bit set</strong>.</p>
<p>In order to fully understand Linux <a href="http://www.gnu.org/software/coreutils/manual/html_node/File-permissions.html#File-permissions">file permissions</a> you can stu... | <p><strong>Permission denied</strong></p> <p>In order to run a script the file must have <strong>an executable permission bit set</strong>.</p> <p>In order to fully understand Linux <a href="http://www.gnu.org/software/coreutils/manual/html_node/File-permissions.html#File-permissions">file permissions</a> you can stu... | 58, 387, 1674 | bash, linux, sudo | <h1>Command not found when using sudo</h1>
<p>I have a script called <code>foo.sh</code> in my home folder. </p>
<p>When I navigate to this folder, and enter <code>./foo.sh</code>, I get </p>
<p><code>-bash: ./foo.sh: Permission denied</code>. </p>
<p>When I use <code>sudo ./foo.sh</code>, I get </p>
<p><code>sudo:... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 986 | bash | # Command not found when using sudo
I have a script called `foo.sh` in my home folder.
When I navigate to this folder, and enter `./foo.sh`, I get
`-bash: ./foo.sh: Permission denied`.
When I use `sudo ./foo.sh`, I get
`sudo: foo.sh: command not found`.
Why does this happen and how I can fix it? | **Permission denied**
In order to run a script the file must have **an executable permission bit set**.
In order to fully understand Linux [file permissions](http://www.gnu.org/software/coreutils/manual/html_node/File-permissions.html#File-permissions) you can study the documentation for the `chmod` command. [chmod](... |
11618696 | Shell - Write variable contents to a file | 172 | 2012-07-23 18:56:42 | <p>I would like to copy the contents of a variable (here called <code>var</code>) into a file.</p>
<p>The name of the file is stored in another variable <code>destfile</code>. </p>
<p>I'm having problems doing this. Here's what I've tried:</p>
<pre><code>cp $var $destfile
</code></pre>
<p>I've also tried the same t... | 463,615 | 1,546,083 | 2024-11-21 02:26:49 | 11,618,931 | 211 | 2012-07-23 19:12:24 | 275,567 | 2015-02-19 19:57:04 | https://stackoverflow.com/q/11618696 | https://stackoverflow.com/a/11618931 | <p>Use the <code>echo</code> command:</p>
<pre><code>var="text to append";
destdir=/some/directory/path/filename
if [ -f "$destdir" ]
then
echo "$var" > "$destdir"
fi
</code></pre>
<p>The <code>if</code> tests that <code>$destdir</code> represents a file.</p>
<p>The <code>></code> appends the text after ... | <p>Use the <code>echo</code> command:</p> <pre><code>var="text to append"; destdir=/some/directory/path/filename if [ -f "$destdir" ] then echo "$var" > "$destdir" fi </code></pre> <p>The <code>if</code> tests that <code>$destdir</code> represents a file.</p> <p>The <code>></code> appends the text after ... | 58, 387, 390 | bash, linux, shell | <h1>Shell - Write variable contents to a file</h1>
<p>I would like to copy the contents of a variable (here called <code>var</code>) into a file.</p>
<p>The name of the file is stored in another variable <code>destfile</code>. </p>
<p>I'm having problems doing this. Here's what I've tried:</p>
<pre><code>cp $var $de... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 987 | bash | # Shell - Write variable contents to a file
I would like to copy the contents of a variable (here called `var`) into a file.
The name of the file is stored in another variable `destfile`.
I'm having problems doing this. Here's what I've tried:
```
cp $var $destfile
```
I've also tried the same thing with the dd co... | Use the `echo` command:
```
var="text to append";
destdir=/some/directory/path/filename
if [ -f "$destdir" ]
then
echo "$var" > "$destdir"
fi
```
The `if` tests that `$destdir` represents a file.
The `>` appends the text after truncating the file. If you only want to append the text in `$var` to the file exist... |
4145232 | Path to Powershell.exe (v 2.0) | 150 | 2010-11-10 13:59:41 | <p>Where is the Powershell (version 2.0) located? What is the path to Powershell.exe? I have Windows Server 2008 and Powershell installed. When I look at this folder:</p>
<pre><code>PS C:\Windows\System32\WindowsPowerShell> dir
Directory: C:\Windows\System32\WindowsPowerShell
Mode LastWriteTi... | 443,380 | 477,087 | 2023-07-10 07:11:24 | 4,145,371 | 211 | 2010-11-10 14:14:12 | 314,547 | 2019-07-19 10:06:47 | https://stackoverflow.com/q/4145232 | https://stackoverflow.com/a/4145371 | <p>I believe it's in <code>C:\Windows\System32\WindowsPowershell\v1.0\</code>. In order to confuse the innocent, MS kept it in a directory labeled "v1.0". Running this on Windows 7 and checking the version number via <code>$Host.Version</code> (<a href="https://stackoverflow.com/questions/1825585/determine-what-version... | <p>I believe it's in <code>C:\Windows\System32\WindowsPowershell\v1.0\</code>. In order to confuse the innocent, MS kept it in a directory labeled "v1.0". Running this on Windows 7 and checking the version number via <code>$Host.Version</code> (<a href="https://stackoverflow.com/questions/1825585/determine-what-version... | 526, 6268 | path, powershell | <h1>Path to Powershell.exe (v 2.0)</h1>
<p>Where is the Powershell (version 2.0) located? What is the path to Powershell.exe? I have Windows Server 2008 and Powershell installed. When I look at this folder:</p>
<pre><code>PS C:\Windows\System32\WindowsPowerShell> dir
Directory: C:\Windows\System32\WindowsPowe... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 988 | bash | # Path to Powershell.exe (v 2.0)
Where is the Powershell (version 2.0) located? What is the path to Powershell.exe? I have Windows Server 2008 and Powershell installed. When I look at this folder:
```
PS C:\Windows\System32\WindowsPowerShell> dir
Directory: C:\Windows\System32\WindowsPowerShell
Mode ... | I believe it's in `C:\Windows\System32\WindowsPowershell\v1.0\`. In order to confuse the innocent, MS kept it in a directory labeled "v1.0". Running this on Windows 7 and checking the version number via `$Host.Version` ([Determine installed PowerShell version](https://stackoverflow.com/questions/1825585/determine-what-... |
20176410 | How to resolve "You need to have Ruby and Sass installed and in your PATH for this task to work" Warning? | 124 | 2013-11-24 15:42:13 | <p>I am in the process of setting up a new Mac for work. I have installed Grunt & Grunt CLI globally. Then I did a <code>npm install</code> inside a project folder to install all dependencies. </p>
<p>No problems so far, but as soon as I try to run the <code>sass:dist</code> task, I get this warning: </p>
<pre>... | 95,794 | 1,144,247 | 2020-08-13 12:09:03 | 20,177,294 | 211 | 2013-11-24 17:04:35 | 1,144,247 | 2013-11-24 17:04:35 | https://stackoverflow.com/q/20176410 | https://stackoverflow.com/a/20177294 | <p>Ok I got it. I just had to install Sass using <code>gem install sass</code> and now everything is fine...couldn't be easier.</p>
| <p>Ok I got it. I just had to install Sass using <code>gem install sass</code> and now everything is fine...couldn't be easier.</p> | 12, 369, 387, 391, 6367 | bash, macos, ruby, sass, terminal | <h1>How to resolve "You need to have Ruby and Sass installed and in your PATH for this task to work" Warning?</h1>
<p>I am in the process of setting up a new Mac for work. I have installed Grunt & Grunt CLI globally. Then I did a <code>npm install</code> inside a project folder to install all dependencies. </p>
<... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 989 | bash | # How to resolve "You need to have Ruby and Sass installed and in your PATH for this task to work" Warning?
I am in the process of setting up a new Mac for work. I have installed Grunt & Grunt CLI globally. Then I did a `npm install` inside a project folder to install all dependencies.
No problems so far, but as soon... | Ok I got it. I just had to install Sass using `gem install sass` and now everything is fine...couldn't be easier. |
219402 | What linux shell command returns a part of a string? | 116 | 2008-10-20 18:33:34 | <p>I want to find a linux command that can return a part of the string. In most programming languages, it's the <code>substr()</code> function. Does bash have any command that can be used for this purpose. I want to be able to do something like this...
<code>substr "abcdefg" 2 3</code> - prints <code>cde</code>.</p>
<... | 259,313 | 15,595 | 2017-02-28 15:12:14 | 219,427 | 211 | 2008-10-20 18:39:35 | 22,329 | 2015-08-28 10:58:58 | https://stackoverflow.com/q/219402 | https://stackoverflow.com/a/219427 | <p>If you are looking for a shell utility to do something like that, you can use the <code>cut</code> command.</p>
<p>To take your example, try:</p>
<pre><code>echo "abcdefg" | cut -c3-5
</code></pre>
<p>which yields</p>
<pre><code>cde
</code></pre>
<p>Where <code>-cN-M</code> tells the cut command to return colum... | <p>If you are looking for a shell utility to do something like that, you can use the <code>cut</code> command.</p> <p>To take your example, try:</p> <pre><code>echo "abcdefg" | cut -c3-5 </code></pre> <p>which yields</p> <pre><code>cde </code></pre> <p>Where <code>-cN-M</code> tells the cut command to return colum... | 58, 139, 387, 13183 | bash, linux, string, substr | <h1>What linux shell command returns a part of a string?</h1>
<p>I want to find a linux command that can return a part of the string. In most programming languages, it's the <code>substr()</code> function. Does bash have any command that can be used for this purpose. I want to be able to do something like this...
<code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 990 | bash | # What linux shell command returns a part of a string?
I want to find a linux command that can return a part of the string. In most programming languages, it's the `substr()` function. Does bash have any command that can be used for this purpose. I want to be able to do something like this...
`substr "abcdefg" 2 3` - ... | If you are looking for a shell utility to do something like that, you can use the `cut` command.
To take your example, try:
```
echo "abcdefg" | cut -c3-5
```
which yields
```
cde
```
Where `-cN-M` tells the cut command to return columns `N` to `M`, inclusive. |
17682934 | Linux Terminal: typing feedback gone, line breaks not displayed | 98 | 2013-07-16 17:13:13 | <p>From time to time I have to run a command-line tool (a Python script) whose output seems to break my terminal.
After the execution is finished, the typing feedback is gone (I can't see what I'm typing), and also line breaks are not displayed. This happens if the terminal is started remotely via <code>Putty</code>, a... | 60,350 | 15,931 | 2025-09-03 08:25:16 | 17,683,085 | 211 | 2013-07-16 17:20:38 | 2,585,788 | 2018-04-12 08:14:24 | https://stackoverflow.com/q/17682934 | https://stackoverflow.com/a/17683085 | <p>Execute the command <code>reset</code> and your terminal should be restored (<a href="http://www.commandlinefu.com/commands/view/32/salvage-a-borked-terminal" rel="noreferrer">reference</a>).</p>
<p>This issue happens generally when dumping binary data to the terminal <code>STDOUT</code> which when the escape codes... | <p>Execute the command <code>reset</code> and your terminal should be restored (<a href="http://www.commandlinefu.com/commands/view/32/salvage-a-borked-terminal" rel="noreferrer">reference</a>).</p> <p>This issue happens generally when dumping binary data to the terminal <code>STDOUT</code> which when the escape codes... | 58, 387, 391, 1768, 32965 | bash, gnome-terminal, linux, putty, terminal | <h1>Linux Terminal: typing feedback gone, line breaks not displayed</h1>
<p>From time to time I have to run a command-line tool (a Python script) whose output seems to break my terminal.
After the execution is finished, the typing feedback is gone (I can't see what I'm typing), and also line breaks are not displayed. T... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 991 | bash | # Linux Terminal: typing feedback gone, line breaks not displayed
From time to time I have to run a command-line tool (a Python script) whose output seems to break my terminal.
After the execution is finished, the typing feedback is gone (I can't see what I'm typing), and also line breaks are not displayed. This happe... | Execute the command `reset` and your terminal should be restored ([reference](http://www.commandlinefu.com/commands/view/32/salvage-a-borked-terminal)).
This issue happens generally when dumping binary data to the terminal `STDOUT` which when the escape codes received are processed can do anything from change the colo... |
20010199 | How to determine if a process runs inside lxc/Docker? | 268 | 2013-11-15 20:42:04 | <p>Is there any way to determine if a process (script) runs inside an lxc container (~ Docker runtime)? I know that some programs are able to detect whether they run inside a virtual machine, is something similar available for lxc/docker?</p>
| 121,888 | 2,216,860 | 2025-08-09 03:27:57 | 20,012,536 | 210 | 2013-11-15 23:36:06 | 580,281 | 2019-07-02 02:44:18 | https://stackoverflow.com/q/20010199 | https://stackoverflow.com/a/20012536 | <p>The most reliable way is to check <code>/proc/1/cgroup</code>. It will tell you the control groups of the init process, and when you are <em>not</em> in a container, that will be <code>/</code> for all hierarchies. When you are <em>inside</em> a container, you will see the name of the anchor point. With LXC/Docker c... | <p>The most reliable way is to check <code>/proc/1/cgroup</code>. It will tell you the control groups of the init process, and when you are <em>not</em> in a container, that will be <code>/</code> for all hierarchies. When you are <em>inside</em> a container, you will see the name of the anchor point. With LXC/Docker c... | 58, 387, 90304 | bash, docker, linux | <h1>How to determine if a process runs inside lxc/Docker?</h1>
<p>Is there any way to determine if a process (script) runs inside an lxc container (~ Docker runtime)? I know that some programs are able to detect whether they run inside a virtual machine, is something similar available for lxc/docker?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 992 | bash | # How to determine if a process runs inside lxc/Docker?
Is there any way to determine if a process (script) runs inside an lxc container (~ Docker runtime)? I know that some programs are able to detect whether they run inside a virtual machine, is something similar available for lxc/docker? | The most reliable way is to check `/proc/1/cgroup`. It will tell you the control groups of the init process, and when you are *not* in a container, that will be `/` for all hierarchies. When you are *inside* a container, you will see the name of the anchor point. With LXC/Docker containers, it will be something like `/... |
13913014 | Grepping a huge file (80GB) any way to speed it up? | 162 | 2012-12-17 11:12:43 | <pre><code> grep -i -A 5 -B 5 'db_pd.Clients' eightygigsfile.sql
</code></pre>
<p>This has been running for an hour on a fairly powerful linux server which is otherwise not overloaded.
Any alternative to grep? Anything about my syntax that can be improved, (egrep,fgrep better?) </p>
<p>The file is actually in a dire... | 124,413 | 94,335 | 2023-06-11 05:48:42 | 13,913,220 | 210 | 2012-12-17 11:25:46 | 7,412 | 2012-12-17 11:25:46 | https://stackoverflow.com/q/13913014 | https://stackoverflow.com/a/13913220 | <p>Here are a few options:</p>
<p>1) Prefix your grep command with <code>LC_ALL=C</code> to use the C locale instead of UTF-8.</p>
<p>2) Use <code>fgrep</code> because you're searching for a fixed string, not a regular expression.</p>
<p>3) Remove the <code>-i</code> option, if you don't need it.</p>
<p>So your com... | <p>Here are a few options:</p> <p>1) Prefix your grep command with <code>LC_ALL=C</code> to use the C locale instead of UTF-8.</p> <p>2) Use <code>fgrep</code> because you're searching for a fixed string, not a regular expression.</p> <p>3) Remove the <code>-i</code> option, if you don't need it.</p> <p>So your com... | 387, 1271 | bash, grep | <h1>Grepping a huge file (80GB) any way to speed it up?</h1>
<pre><code> grep -i -A 5 -B 5 'db_pd.Clients' eightygigsfile.sql
</code></pre>
<p>This has been running for an hour on a fairly powerful linux server which is otherwise not overloaded.
Any alternative to grep? Anything about my syntax that can be improved, ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 993 | bash | # Grepping a huge file (80GB) any way to speed it up?
```
grep -i -A 5 -B 5 'db_pd.Clients' eightygigsfile.sql
```
This has been running for an hour on a fairly powerful linux server which is otherwise not overloaded.
Any alternative to grep? Anything about my syntax that can be improved, (egrep,fgrep better?)
The... | Here are a few options:
1) Prefix your grep command with `LC_ALL=C` to use the C locale instead of UTF-8.
2) Use `fgrep` because you're searching for a fixed string, not a regular expression.
3) Remove the `-i` option, if you don't need it.
So your command becomes:
```
LC_ALL=C fgrep -A 5 -B 5 'db_pd.Clients' eigh... |
22846596 | What does "%" (percent) do in PowerShell? | 160 | 2014-04-03 19:01:11 | <p>It seems that the % operation starts script blocks after the pipeline, although <a href="http://technet.microsoft.com/en-us/library/hh847893.aspx" rel="noreferrer">about_Script_Blocks</a> indicates the % isn't necessary.</p>
<p>These all work just fine.</p>
<pre><code>get-childitem | % { write-host $_.Name }
{ wr... | 128,431 | 1,108,891 | 2020-02-16 06:07:18 | 22,846,633 | 210 | 2014-04-03 19:03:21 | 3,396,598 | 2014-04-03 19:41:17 | https://stackoverflow.com/q/22846596 | https://stackoverflow.com/a/22846633 | <p>When used in the context of a cmdlet (such as your example), it's an alias for <code>ForEach-Object</code>:</p>
<pre><code>> Get-Alias -Definition ForEach-Object
CommandType Name Definition
----------- ---- ---... | <p>When used in the context of a cmdlet (such as your example), it's an alias for <code>ForEach-Object</code>:</p> <pre><code>> Get-Alias -Definition ForEach-Object CommandType Name Definition ----------- ---- ---... | 367, 526 | powershell, syntax | <h1>What does "%" (percent) do in PowerShell?</h1>
<p>It seems that the % operation starts script blocks after the pipeline, although <a href="http://technet.microsoft.com/en-us/library/hh847893.aspx" rel="noreferrer">about_Script_Blocks</a> indicates the % isn't necessary.</p>
<p>These all work just fine.</p>
<pre><... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 994 | bash | # What does "%" (percent) do in PowerShell?
It seems that the % operation starts script blocks after the pipeline, although [about_Script_Blocks](http://technet.microsoft.com/en-us/library/hh847893.aspx) indicates the % isn't necessary.
These all work just fine.
```
get-childitem | % { write-host $_.Name }
{ write-... | When used in the context of a cmdlet (such as your example), it's an alias for `ForEach-Object`:
```
> Get-Alias -Definition ForEach-Object
CommandType Name Definition
----------- ---- ----------
Alias % ... |
1069302 | Using the star sign in grep | 155 | 2009-07-01 14:06:58 | <p>I am trying to search for the substring "abc" in a specific file in linux/bash</p>
<p>So I do:</p>
<pre><code>grep '*abc*' myFile
</code></pre>
<p>It returns nothing.</p>
<p>But if I do:</p>
<pre><code>grep 'abc' myFile
</code></pre>
<p>It returns matches correctly.</p>
<p>Now, this is not a problem for me. B... | 351,453 | 104,459 | 2023-06-24 09:06:22 | 1,069,333 | 210 | 2009-07-01 14:12:01 | 92,313 | 2023-06-24 09:06:22 | https://stackoverflow.com/q/1069302 | https://stackoverflow.com/a/1069333 | <p>For such two-part matches, use <code>.*</code> between the two parts.</p>
<p>For instance:</p>
<p><code>grep 'abc.*def' myFile</code></p>
<p>will match a string that contains <code>abc</code> followed by <code>def</code> with something optionally in between.</p>
<hr />
<p>The asterisk is just a <a href="http://www.r... | <p>For such two-part matches, use <code>.*</code> between the two parts.</p> <p>For instance:</p> <p><code>grep 'abc.*def' myFile</code></p> <p>will match a string that contains <code>abc</code> followed by <code>def</code> with something optionally in between.</p> <hr /> <p>The asterisk is just a <a href="http://www.r... | 18, 387, 1271 | bash, grep, regex | <h1>Using the star sign in grep</h1>
<p>I am trying to search for the substring "abc" in a specific file in linux/bash</p>
<p>So I do:</p>
<pre><code>grep '*abc*' myFile
</code></pre>
<p>It returns nothing.</p>
<p>But if I do:</p>
<pre><code>grep 'abc' myFile
</code></pre>
<p>It returns matches correctly.</p>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 995 | bash | # Using the star sign in grep
I am trying to search for the substring "abc" in a specific file in linux/bash
So I do:
```
grep '*abc*' myFile
```
It returns nothing.
But if I do:
```
grep 'abc' myFile
```
It returns matches correctly.
Now, this is not a problem for me. But what if I want to grep for a more comp... | For such two-part matches, use `.*` between the two parts.
For instance:
`grep 'abc.*def' myFile`
will match a string that contains `abc` followed by `def` with something optionally in between.
---
The asterisk is just a [repetition operator](http://www.regular-expressions.info/repeat.html), but you need to tell i... |
51247619 | Execute bash command in pod with kubectl? | 118 | 2018-07-09 14:10:47 | <p>How to execute a Bash command in the pod? I want to do everything with a single Bash command.</p>
<pre><code>[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod --bash -c "mongo"
Error: unknown flag: --bash
</code></pre>
<p>So, the command is simply ignored.</p>
<pre><code>[root@maste... | 341,990 | 1,590,594 | 2025-06-19 06:01:46 | 51,247,696 | 210 | 2018-07-09 14:14:45 | 2,718,151 | 2021-06-08 19:37:59 | https://stackoverflow.com/q/51247619 | https://stackoverflow.com/a/51247696 | <p>The double dash symbol "--" is used to separate the command you want to run inside the container from the kubectl arguments.
So the correct way is:</p>
<pre><code>kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo"
</code></pre>
<p>You forgot a space between "--" and "b... | <p>The double dash symbol "--" is used to separate the command you want to run inside the container from the kubectl arguments. So the correct way is:</p> <pre><code>kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo" </code></pre> <p>You forgot a space between "--" and "b... | 387, 106338, 120010 | bash, kubectl, kubernetes | <h1>Execute bash command in pod with kubectl?</h1>
<p>How to execute a Bash command in the pod? I want to do everything with a single Bash command.</p>
<pre><code>[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod --bash -c "mongo"
Error: unknown flag: --bash
</code></pre>
<p>So, the co... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 996 | bash | # Execute bash command in pod with kubectl?
How to execute a Bash command in the pod? I want to do everything with a single Bash command.
```
[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod --bash -c "mongo"
Error: unknown flag: --bash
```
So, the command is simply ignored.
```
[root@master ~]# kube... | The double dash symbol "--" is used to separate the command you want to run inside the container from the kubectl arguments.
So the correct way is:
```
kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo"
```
You forgot a space between "--" and "bash".
To execute multiple commands you may want:
- to crea... |
49328525 | pip install dotenv error code 1 Windows 10 | 67 | 2018-03-16 19:52:51 | <p>I am in a virtualenv and trying to run through pip installs. I know the code works because outside the virtualenv this code has worked. I am running on a Windows 10 machine. Using the Git Bash terminal or the regular command prompt (have tried as admin and regular user).
I am trying to run
<code>pip install dotenv<... | 101,462 | 8,796,357 | 2024-02-28 20:41:16 | 49,328,725 | 210 | 2018-03-16 20:05:08 | 7,907,591 | 2020-08-07 16:39:41 | https://stackoverflow.com/q/49328525 | https://stackoverflow.com/a/49328725 | <p>You should install <code>python-dotenv</code></p>
<pre class="lang-sh prettyprint-override"><code>pip3 install python-dotenv
</code></pre>
<p>or</p>
<pre class="lang-sh prettyprint-override"><code>pip install python-dotenv
</code></pre>
<p>i.e</p>
<pre><code>C:\Users\USER>pip3 install python-dotenv
Collecting pyt... | <p>You should install <code>python-dotenv</code></p> <pre class="lang-sh prettyprint-override"><code>pip3 install python-dotenv </code></pre> <p>or</p> <pre class="lang-sh prettyprint-override"><code>pip install python-dotenv </code></pre> <p>i.e</p> <pre><code>C:\Users\USER>pip3 install python-dotenv Collecting pyt... | 16, 64, 390, 14529 | python, shell, virtualenv, windows | <h1>pip install dotenv error code 1 Windows 10</h1>
<p>I am in a virtualenv and trying to run through pip installs. I know the code works because outside the virtualenv this code has worked. I am running on a Windows 10 machine. Using the Git Bash terminal or the regular command prompt (have tried as admin and regular ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 997 | bash | # pip install dotenv error code 1 Windows 10
I am in a virtualenv and trying to run through pip installs. I know the code works because outside the virtualenv this code has worked. I am running on a Windows 10 machine. Using the Git Bash terminal or the regular command prompt (have tried as admin and regular user).
I ... | You should install `python-dotenv`
```
pip3 install python-dotenv
```
or
```
pip install python-dotenv
```
i.e
```
C:\Users\USER>pip3 install python-dotenv
Collecting python-dotenv
Downloading python_dotenv-0.8.2-py2.py3-none-any.whl
Installing collected packages: python-dotenv
Successfully installed python-dote... |
4381618 | Exiting a script upon encountering an error | 208 | 2010-12-07 21:10:37 | <p>I'm building a shell script that has an <code>if</code> statement like this one:</p>
<pre><code>if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
echo $jar_file signed sucessfully
else
echo ERROR: Failed to sign $jar_file. Please recheck the variables
fi
...
</code></pre>
<p>I wa... | 287,177 | 126,353 | 2024-11-27 20:31:57 | 4,381,649 | 209 | 2010-12-07 21:13:49 | 42,304 | 2014-04-23 09:13:35 | https://stackoverflow.com/q/4381618 | https://stackoverflow.com/a/4381649 | <p>Are you looking for <a href="http://tldp.org/LDP/abs/html/exit-status.html"><code>exit</code></a>?</p>
<p>This is the best bash guide around.
<a href="http://tldp.org/LDP/abs/html/">http://tldp.org/LDP/abs/html/</a></p>
<p>In context:</p>
<pre><code>if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file... | <p>Are you looking for <a href="http://tldp.org/LDP/abs/html/exit-status.html"><code>exit</code></a>?</p> <p>This is the best bash guide around. <a href="http://tldp.org/LDP/abs/html/">http://tldp.org/LDP/abs/html/</a></p> <p>In context:</p> <pre><code>if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file... | 387, 390, 10871 | bash, exit, shell | <h1>Exiting a script upon encountering an error</h1>
<p>I'm building a shell script that has an <code>if</code> statement like this one:</p>
<pre><code>if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
echo $jar_file signed sucessfully
else
echo ERROR: Failed to sign $jar_file. Pleas... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 998 | bash | # Exiting a script upon encountering an error
I'm building a shell script that has an `if` statement like this one:
```
if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
echo $jar_file signed sucessfully
else
echo ERROR: Failed to sign $jar_file. Please recheck the variables
fi
..... | Are you looking for [`exit`](http://tldp.org/LDP/abs/html/exit-status.html)?
This is the best bash guide around.
<http://tldp.org/LDP/abs/html/>
In context:
```
if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
echo $jar_file signed sucessfully
else
echo ERROR: Failed to sign $jar... |
10982911 | Creating temporary files in bash | 185 | 2012-06-11 15:15:06 | <p>Are there objectively better ways to create temporary files in bash scripts?</p>
<p>I normally just name them whatever comes to my mind, such as tempfile-123, since it will be deleted when the script is over. Is there any disadvantage in doing this other than overwriting a possible tempfile-123 in current folder? O... | 145,877 | 882,056 | 2022-10-28 09:39:09 | 10,983,009 | 209 | 2012-06-11 15:20:51 | 418,413 | 2017-09-08 17:16:39 | https://stackoverflow.com/q/10982911 | https://stackoverflow.com/a/10983009 | <p>The <code>mktemp(1)</code> man page explains it fairly well:</p>
<blockquote>
<p>Traditionally, many shell scripts take the name of the program with
the pid as a suffix and use that as a temporary file name. This kind
of naming scheme is predictable and the race condition it creates is
easy for an attacker ... | <p>The <code>mktemp(1)</code> man page explains it fairly well:</p> <blockquote> <p>Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker ... | 34, 58, 387 | bash, linux, unix | <h1>Creating temporary files in bash</h1>
<p>Are there objectively better ways to create temporary files in bash scripts?</p>
<p>I normally just name them whatever comes to my mind, such as tempfile-123, since it will be deleted when the script is over. Is there any disadvantage in doing this other than overwriting a ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 999 | bash | # Creating temporary files in bash
Are there objectively better ways to create temporary files in bash scripts?
I normally just name them whatever comes to my mind, such as tempfile-123, since it will be deleted when the script is over. Is there any disadvantage in doing this other than overwriting a possible tempfil... | The `mktemp(1)` man page explains it fairly well:
> Traditionally, many shell scripts take the name of the program with
> the pid as a suffix and use that as a temporary file name. This kind
> of naming scheme is predictable and the race condition it creates is
> easy for an attacker to win. A safer, though still infe... |
8388650 | Powershell: How can I stop errors from being displayed in a script? | 151 | 2011-12-05 16:41:17 | <p>When my PowerShell script tries, for example, to create a SQL Server object for a server that doesn't exist ("bla" in my case), PowerShell displays lots of PowerShell errors in red.</p>
<p>Since my script checks the value of <code>$?</code> after such calls, and displays and logs errors, I'd rather not have the sev... | 441,569 | 4,886 | 2025-07-16 20:23:54 | 8,388,822 | 209 | 2011-12-05 16:54:18 | 397,952 | 2018-10-18 07:26:44 | https://stackoverflow.com/q/8388650 | https://stackoverflow.com/a/8388822 | <p>You have a couple of options. The easiest involve using the <code>ErrorAction</code> settings.</p>
<p><code>-Erroraction</code> is a universal parameter for all cmdlets. If there are special commands you want to ignore you can use <code>-erroraction 'silentlycontinue'</code> which will basically ignore all error ... | <p>You have a couple of options. The easiest involve using the <code>ErrorAction</code> settings.</p> <p><code>-Erroraction</code> is a universal parameter for all cmdlets. If there are special commands you want to ignore you can use <code>-erroraction 'silentlycontinue'</code> which will basically ignore all error ... | 379, 526 | error-handling, powershell | <h1>Powershell: How can I stop errors from being displayed in a script?</h1>
<p>When my PowerShell script tries, for example, to create a SQL Server object for a server that doesn't exist ("bla" in my case), PowerShell displays lots of PowerShell errors in red.</p>
<p>Since my script checks the value of <code>$?</code... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,000 | bash | # Powershell: How can I stop errors from being displayed in a script?
When my PowerShell script tries, for example, to create a SQL Server object for a server that doesn't exist ("bla" in my case), PowerShell displays lots of PowerShell errors in red.
Since my script checks the value of `$?` after such calls, and dis... | You have a couple of options. The easiest involve using the `ErrorAction` settings.
`-Erroraction` is a universal parameter for all cmdlets. If there are special commands you want to ignore you can use `-erroraction 'silentlycontinue'` which will basically ignore all error messages generated by that command. You can a... |
18704222 | Command to clear the Git Bash screen, including output buffer | 117 | 2013-09-09 18:03:19 | <p>Is there any command in Git, that clear the screen. for example in window command line after execute a lot of code, if you type cls, then it will clear all the previous code. so i want the same type of functionality in Git. so can anybody will tell me the command name.</p>
| 149,156 | 1,614,271 | 2024-12-11 15:45:31 | 19,807,325 | 209 | 2013-11-06 08:38:40 | 2,661,852 | 2023-01-04 09:00:19 | https://stackoverflow.com/q/18704222 | https://stackoverflow.com/a/19807325 | <p>Actually you are looking for a Unix user environment command</p>
<pre><code>clear
</code></pre>
<p>or you can use the keyboard shortcut</p>
<p><kbd>ctrl</kbd>+<kbd>l</kbd></p>
<p><a href="http://en.wikipedia.org/wiki/Clear_(Unix)" rel="noreferrer">http://en.wikipedia.org/wiki/Clear_(Unix)</a></p>
<p>To clear entire ... | <p>Actually you are looking for a Unix user environment command</p> <pre><code>clear </code></pre> <p>or you can use the keyboard shortcut</p> <p><kbd>ctrl</kbd>+<kbd>l</kbd></p> <p><a href="http://en.wikipedia.org/wiki/Clear_(Unix)" rel="noreferrer">http://en.wikipedia.org/wiki/Clear_(Unix)</a></p> <p>To clear entire ... | 456, 6452, 61874 | git-bash, git-svn, version-control | <h1>Command to clear the Git Bash screen, including output buffer</h1>
<p>Is there any command in Git, that clear the screen. for example in window command line after execute a lot of code, if you type cls, then it will clear all the previous code. so i want the same type of functionality in Git. so can anybody will te... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,001 | bash | # Command to clear the Git Bash screen, including output buffer
Is there any command in Git, that clear the screen. for example in window command line after execute a lot of code, if you type cls, then it will clear all the previous code. so i want the same type of functionality in Git. so can anybody will tell me the... | Actually you are looking for a Unix user environment command
```
clear
```
or you can use the keyboard shortcut
`ctrl`+`l`
<http://en.wikipedia.org/wiki/Clear_(Unix)>
To clear entire command history in Git Bash.
```
history -c
``` |
5693360 | Why is an MD5 hash created by Python different from one created using echo and md5sum in the shell? | 117 | 2011-04-17 11:54:05 | <p>A Python MD5 hash is different than the one created by the md5sum command on the shell. Why?</p>
<pre><code>>>> import hashlib
>>> h = hashlib.md5()
>>> h.update("mystringforhash")
>>> print h.hexdigest()
86b6423cb6d211734fc7d81bbc5e11d3 # Result from Python
$ echo mystringforh... | 12,357 | 712,081 | 2012-04-18 19:14:13 | 5,693,363 | 209 | 2011-04-17 11:55:02 | 298,479 | 2012-04-18 19:14:13 | https://stackoverflow.com/q/5693360 | https://stackoverflow.com/a/5693363 | <p><code>echo</code> appends a <code>\n</code> since you usually do not want lines not ending with a linebreak in your shell (it looks really ugly if the prompt does not start at the very left).<br>
Use the <code>-n</code> argument to omit the trailing linebreak and it will print the same checksum as your python script... | <p><code>echo</code> appends a <code>\n</code> since you usually do not want lines not ending with a linebreak in your shell (it looks really ugly if the prompt does not start at the very left).<br> Use the <code>-n</code> argument to omit the trailing linebreak and it will print the same checksum as your python script... | 16, 390, 46308 | md5sum, python, shell | <h1>Why is an MD5 hash created by Python different from one created using echo and md5sum in the shell?</h1>
<p>A Python MD5 hash is different than the one created by the md5sum command on the shell. Why?</p>
<pre><code>>>> import hashlib
>>> h = hashlib.md5()
>>> h.update("mystringforhash")... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,002 | bash | # Why is an MD5 hash created by Python different from one created using echo and md5sum in the shell?
A Python MD5 hash is different than the one created by the md5sum command on the shell. Why?
```
>>> import hashlib
>>> h = hashlib.md5()
>>> h.update("mystringforhash")
>>> print h.hexdigest()
86b6423cb6d211734fc7d8... | `echo` appends a `\n` since you usually do not want lines not ending with a linebreak in your shell (it looks really ugly if the prompt does not start at the very left).
Use the `-n` argument to omit the trailing linebreak and it will print the same checksum as your python script:
```
> echo -n mystringforhash | md5... |
13910087 | Shell script to capture Process ID and kill it if exist | 88 | 2012-12-17 07:40:02 | <p>I tried this code and it is not working</p>
<pre><code>#!/bin/sh
#Find the Process ID for syncapp running instance
PID=`ps -ef | grep syncapp 'awk {print $2}'`
if [[ -z "$PID" ]] then
Kill -9 PID
fi
</code></pre>
<p>It is showing a error near awk.</p>
<p>Any suggestions please.</p>
| 310,704 | 1,597,811 | 2022-12-26 01:19:45 | 15,896,729 | 209 | 2013-04-09 08:16:49 | 2,260,749 | 2022-12-26 01:19:45 | https://stackoverflow.com/q/13910087 | https://stackoverflow.com/a/15896729 | <p>Actually the easiest way to do that would be to pass kill arguments like below:</p>
<pre><code>ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
</code></pre>
| <p>Actually the easiest way to do that would be to pass kill arguments like below:</p> <pre><code>ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill </code></pre> | 387, 390, 531 | bash, scripting, shell | <h1>Shell script to capture Process ID and kill it if exist</h1>
<p>I tried this code and it is not working</p>
<pre><code>#!/bin/sh
#Find the Process ID for syncapp running instance
PID=`ps -ef | grep syncapp 'awk {print $2}'`
if [[ -z "$PID" ]] then
Kill -9 PID
fi
</code></pre>
<p>It is showing a error near awk.... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,003 | bash | # Shell script to capture Process ID and kill it if exist
I tried this code and it is not working
```
#!/bin/sh
#Find the Process ID for syncapp running instance
PID=`ps -ef | grep syncapp 'awk {print $2}'`
if [[ -z "$PID" ]] then
Kill -9 PID
fi
```
It is showing a error near awk.
Any suggestions please. | Actually the easiest way to do that would be to pass kill arguments like below:
```
ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
``` |
7577615 | Parallel wget in Bash | 85 | 2011-09-28 02:12:35 | <p>I am getting a bunch of relatively small pages from a website and was wondering if I could somehow do it in parallel in Bash. Currently my code looks like this, but it takes a while to execute (I think what is slowing me down is the latency in the connection).</p>
<pre><code>for i in {1..42}
do
wget "https://ww... | 77,402 | 824,954 | 2020-09-04 13:08:36 | 11,850,469 | 209 | 2012-08-07 16:42:58 | 572,743 | 2013-01-21 11:09:02 | https://stackoverflow.com/q/7577615 | https://stackoverflow.com/a/11850469 | <p>Much preferrable to pushing <code>wget</code> into the background using <code>&</code> or <code>-b</code>, you can use <code>xargs</code> to the same effect, and better.</p>
<p>The advantage is that <code>xargs</code> will <strong>synchronize properly</strong> with no extra work. Which means that you are safe t... | <p>Much preferrable to pushing <code>wget</code> into the background using <code>&</code> or <code>-b</code>, you can use <code>xargs</code> to the same effect, and better.</p> <p>The advantage is that <code>xargs</code> will <strong>synchronize properly</strong> with no extra work. Which means that you are safe t... | 353, 387, 5583 | bash, parallel-processing, wget | <h1>Parallel wget in Bash</h1>
<p>I am getting a bunch of relatively small pages from a website and was wondering if I could somehow do it in parallel in Bash. Currently my code looks like this, but it takes a while to execute (I think what is slowing me down is the latency in the connection).</p>
<pre><code>for i in ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,004 | bash | # Parallel wget in Bash
I am getting a bunch of relatively small pages from a website and was wondering if I could somehow do it in parallel in Bash. Currently my code looks like this, but it takes a while to execute (I think what is slowing me down is the latency in the connection).
```
for i in {1..42}
do
wget ... | Much preferrable to pushing `wget` into the background using `&` or `-b`, you can use `xargs` to the same effect, and better.
The advantage is that `xargs` will **synchronize properly** with no extra work. Which means that you are safe to access the downloaded files (assuming no error occurs). All downloads will have ... |
1055671 | How can I get the behavior of GNU's readlink -f on a Mac? | 458 | 2009-06-28 20:26:33 | <p>On Linux, the <code>readlink</code> utility accepts an option <code>-f</code> that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be?</p>
<p>Here's some debug information:</p>
<pre><code>$ which readlink; readlink -f
/usr/bin/readlink
readlink: ... | 174,752 | 18,180 | 2022-08-13 11:46:48 | 1,116,890 | 208 | 2009-07-12 20:51:37 | 12,347 | 2016-08-08 19:19:45 | https://stackoverflow.com/q/1055671 | https://stackoverflow.com/a/1116890 | <p><code>readlink -f</code> does two things:</p>
<ol>
<li>It iterates along a sequence of symlinks until it finds an actual file.</li>
<li>It returns that file's <i>canonicalized</i> name—i.e., its absolute pathname.</li>
</ol>
<p>If you want to, you can just build a shell script that uses vanilla readlink beha... | <p><code>readlink -f</code> does two things:</p> <ol> <li>It iterates along a sequence of symlinks until it finds an actual file.</li> <li>It returns that file's <i>canonicalized</i> name—i.e., its absolute pathname.</li> </ol> <p>If you want to, you can just build a shell script that uses vanilla readlink beha... | 369, 2796, 10327 | freebsd, macos, sh | <h1>How can I get the behavior of GNU's readlink -f on a Mac?</h1>
<p>On Linux, the <code>readlink</code> utility accepts an option <code>-f</code> that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be?</p>
<p>Here's some debug information:</p>
<p... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,005 | bash | # How can I get the behavior of GNU's readlink -f on a Mac?
On Linux, the `readlink` utility accepts an option `-f` that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be?
Here's some debug information:
```
$ which readlink; readlink -f
/usr/bin/r... | `readlink -f` does two things:
1. It iterates along a sequence of symlinks until it finds an actual file.
2. It returns that file's *canonicalized* name—i.e., its absolute pathname.
If you want to, you can just build a shell script that uses vanilla readlink behavior to achieve the same thing. Here's an example. Obvi... |
31578446 | Running a script inside a docker container using shell script | 173 | 2015-07-23 04:57:07 | <p>I am trying to create a shell script for setting up a docker container. My script file looks like:</p>
<pre><code>#!bin/bash
docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash
</code></pre>
<p>Running this script file will run the container in a newly invoked bash.</p>
<p>Now I... | 551,333 | 4,554,967 | 2025-12-05 13:38:34 | 31,578,527 | 208 | 2015-07-23 05:04:14 | 2,862,098 | 2020-10-14 20:14:46 | https://stackoverflow.com/q/31578446 | https://stackoverflow.com/a/31578527 | <p>You can run a command in a running container using <code>docker exec [OPTIONS] CONTAINER COMMAND [ARG...]</code>:</p>
<pre><code>docker exec mycontainer /path/to/test.sh
</code></pre>
<p>And to run from a bash session:</p>
<pre><code>docker exec -it mycontainer /bin/bash
</code></pre>
<p>From there you can run your ... | <p>You can run a command in a running container using <code>docker exec [OPTIONS] CONTAINER COMMAND [ARG...]</code>:</p> <pre><code>docker exec mycontainer /path/to/test.sh </code></pre> <p>And to run from a bash session:</p> <pre><code>docker exec -it mycontainer /bin/bash </code></pre> <p>From there you can run your ... | 387, 390, 90304 | bash, docker, shell | <h1>Running a script inside a docker container using shell script</h1>
<p>I am trying to create a shell script for setting up a docker container. My script file looks like:</p>
<pre><code>#!bin/bash
docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash
</code></pre>
<p>Running this sc... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,006 | bash | # Running a script inside a docker container using shell script
I am trying to create a shell script for setting up a docker container. My script file looks like:
```
#!bin/bash
docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash
```
Running this script file will run the container ... | You can run a command in a running container using `docker exec [OPTIONS] CONTAINER COMMAND [ARG...]`:
```
docker exec mycontainer /path/to/test.sh
```
And to run from a bash session:
```
docker exec -it mycontainer /bin/bash
```
From there you can run your script. |
6475524 | How do I prevent commands from showing up in Bash history? | 153 | 2011-06-25 03:01:44 | <p>Sometimes, when I run commands like <code>rm -rf XYZ</code>, I don't want this to be recorded in Bash history, because I might accidentally run the same command again by <code>reverse-i-search</code>. Is there a good way to prevent this from happening?</p>
| 99,599 | 133,973 | 2021-03-13 19:50:44 | 29,188,490 | 208 | 2015-03-21 21:53:22 | 55,075 | 2017-02-27 21:24:57 | https://stackoverflow.com/q/6475524 | https://stackoverflow.com/a/29188490 | <p>If you've set the <code>HISTCONTROL</code> environment variable to <code>ignoreboth</code> (which is usually set by default), commands with a leading space character will not be stored in the history (as well as duplicates).</p>
<p>For example:</p>
<pre><code>$ HISTCONTROL=ignoreboth
$ echo test1
$ echo test2
$ his... | <p>If you've set the <code>HISTCONTROL</code> environment variable to <code>ignoreboth</code> (which is usually set by default), commands with a leading space character will not be stored in the history (as well as duplicates).</p> <p>For example:</p> <pre><code>$ HISTCONTROL=ignoreboth $ echo test1 $ echo test2 $ his... | 387 | bash | <h1>How do I prevent commands from showing up in Bash history?</h1>
<p>Sometimes, when I run commands like <code>rm -rf XYZ</code>, I don't want this to be recorded in Bash history, because I might accidentally run the same command again by <code>reverse-i-search</code>. Is there a good way to prevent this from happeni... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,007 | bash | # How do I prevent commands from showing up in Bash history?
Sometimes, when I run commands like `rm -rf XYZ`, I don't want this to be recorded in Bash history, because I might accidentally run the same command again by `reverse-i-search`. Is there a good way to prevent this from happening? | If you've set the `HISTCONTROL` environment variable to `ignoreboth` (which is usually set by default), commands with a leading space character will not be stored in the history (as well as duplicates).
For example:
```
$ HISTCONTROL=ignoreboth
$ echo test1
$ echo test2
$ history | tail -n2
1015 echo test1
1016 ... |
1853946 | Getting the last argument passed to a shell script | 356 | 2009-12-05 23:58:13 | <p><code>$1</code> is the first argument.<br>
<code>$@</code> is all of them.</p>
<p>How can I find the last argument passed to a shell
script?</p>
| 182,873 | 209,573 | 2025-11-13 00:52:47 | 1,853,993 | 207 | 2009-12-06 00:16:23 | 90,848 | 2009-12-06 00:16:23 | https://stackoverflow.com/q/1853946 | https://stackoverflow.com/a/1853993 | <p>This is a bit of a hack:</p>
<pre><code>for last; do true; done
echo $last
</code></pre>
<p>This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.</p>
<p>It uses the fact that <code>for</code> implicitly loops over the arguments if you ... | <p>This is a bit of a hack:</p> <pre><code>for last; do true; done echo $last </code></pre> <p>This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.</p> <p>It uses the fact that <code>for</code> implicitly loops over the arguments if you ... | 390, 2313 | arguments, shell | <h1>Getting the last argument passed to a shell script</h1>
<p><code>$1</code> is the first argument.<br>
<code>$@</code> is all of them.</p>
<p>How can I find the last argument passed to a shell
script?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,008 | bash | # Getting the last argument passed to a shell script
`$1` is the first argument.
`$@` is all of them.
How can I find the last argument passed to a shell
script? | This is a bit of a hack:
```
for last; do true; done
echo $last
```
This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.
It uses the fact that `for` implicitly loops over the arguments if you don't tell it what to loop over, and the fact... |
47105490 | Can I pass a string variable to jq rather than passing a file? | 126 | 2017-11-03 22:55:24 | <p>I want to convert JSON string into an array in bash.
The JSON string is passed to the bash script as an argument (it doesn't exist in a file).</p>
<p>Is there a way of achieving it without using some temp files?</p>
<p>Similarly to this:</p>
<pre><code>script.sh
#! /bin/bash
json_data='{"key":"value"}'
jq '.key'... | 132,855 | 6,260,775 | 2025-08-14 22:39:02 | 47,105,805 | 207 | 2017-11-03 23:36:17 | 8,379,597 | 2017-11-03 23:36:17 | https://stackoverflow.com/q/47105490 | https://stackoverflow.com/a/47105805 | <p>I would suggest using a bash <a href="https://linux.die.net/abs-guide/x15683.html" rel="noreferrer">here string</a>. e.g.</p>
<pre><code>jq '.key' <<< "$json_data"
</code></pre>
| <p>I would suggest using a bash <a href="https://linux.die.net/abs-guide/x15683.html" rel="noreferrer">here string</a>. e.g.</p> <pre><code>jq '.key' <<< "$json_data" </code></pre> | 276, 387, 1508, 5486, 105170 | bash, input, jq, json, variables | <h1>Can I pass a string variable to jq rather than passing a file?</h1>
<p>I want to convert JSON string into an array in bash.
The JSON string is passed to the bash script as an argument (it doesn't exist in a file).</p>
<p>Is there a way of achieving it without using some temp files?</p>
<p>Similarly to this:</p>
... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,009 | bash | # Can I pass a string variable to jq rather than passing a file?
I want to convert JSON string into an array in bash.
The JSON string is passed to the bash script as an argument (it doesn't exist in a file).
Is there a way of achieving it without using some temp files?
Similarly to this:
```
script.sh
#! /bin/bash... | I would suggest using a bash [here string](https://linux.die.net/abs-guide/x15683.html). e.g.
```
jq '.key' <<< "$json_data"
``` |
8343767 | How to get the current directory of the cmdlet being executed | 286 | 2011-12-01 15:23:53 | <p>This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute <code>C:\temp\myscripts\mycmdlet.ps1</code> which has a settings file at <code>C:\temp\myscripts\settings.xml</code> I would lik... | 573,820 | 437,859 | 2022-09-30 04:46:10 | 8,406,946 | 206 | 2011-12-06 21:15:05 | 64,046 | 2019-06-18 00:31:15 | https://stackoverflow.com/q/8343767 | https://stackoverflow.com/a/8406946 | <p>The reliable way to do this is just like you showed <code>$MyInvocation.MyCommand.Path</code>.</p>
<p>Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.</p>
<p><strong>PowerShell v3+</strong>:</p>
<p>Use the automat... | <p>The reliable way to do this is just like you showed <code>$MyInvocation.MyCommand.Path</code>.</p> <p>Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.</p> <p><strong>PowerShell v3+</strong>:</p> <p>Use the automat... | 526, 153947 | powershell, powershell-cmdlet | <h1>How to get the current directory of the cmdlet being executed</h1>
<p>This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute <code>C:\temp\myscripts\mycmdlet.ps1</code> which has a s... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,010 | bash | # How to get the current directory of the cmdlet being executed
This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute `C:\temp\myscripts\mycmdlet.ps1` which has a settings file at `C:\... | The reliable way to do this is just like you showed `$MyInvocation.MyCommand.Path`.
Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.
**PowerShell v3+**:
Use the automatic variable `$PSScriptRoot`. |
18404272 | Running SSH Agent when starting Git Bash on Windows | 221 | 2013-08-23 13:37:05 | <p>I am using git bash. I have to use</p>
<pre><code>eval `ssh-agent.exe`
ssh-add /my/ssh/location/
</code></pre>
<p>every time when I start a new git bash.</p>
<p>Is there a way to set ssh agent permanently? Or does windows has a good way
to manage the ssh keys?</p>
<p>I'm a new guy, please give me detailed tutori... | 290,241 | 2,711,186 | 2025-01-08 13:47:47 | 18,404,557 | 206 | 2013-08-23 13:50:41 | 6,309 | 2025-01-08 13:47:47 | https://stackoverflow.com/q/18404272 | https://stackoverflow.com/a/18404557 | <p>2013: In a <code>git bash</code> session, you can add a script to <code>~/.profile</code> or <code>~/.bashrc</code> (<a href="https://stackoverflow.com/a/3455231/6309">with <code>~</code> being usually set to <code>%USERPROFILE%</code></a>), in order for said session to launch automatically the <code>ssh-agent</code... | <p>2013: In a <code>git bash</code> session, you can add a script to <code>~/.profile</code> or <code>~/.bashrc</code> (<a href="https://stackoverflow.com/a/3455231/6309">with <code>~</code> being usually set to <code>%USERPROFILE%</code></a>), in order for said session to launch automatically the <code>ssh-agent</code... | 64, 386, 61874 | git-bash, ssh, windows | <h1>Running SSH Agent when starting Git Bash on Windows</h1>
<p>I am using git bash. I have to use</p>
<pre><code>eval `ssh-agent.exe`
ssh-add /my/ssh/location/
</code></pre>
<p>every time when I start a new git bash.</p>
<p>Is there a way to set ssh agent permanently? Or does windows has a good way
to manage the ss... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,011 | bash | # Running SSH Agent when starting Git Bash on Windows
I am using git bash. I have to use
```
eval `ssh-agent.exe`
ssh-add /my/ssh/location/
```
every time when I start a new git bash.
Is there a way to set ssh agent permanently? Or does windows has a good way
to manage the ssh keys?
I'm a new guy, please give me d... | 2013: In a `git bash` session, you can add a script to `~/.profile` or `~/.bashrc` ([with `~` being usually set to `%USERPROFILE%`](https://stackoverflow.com/a/3455231/6309)), in order for said session to launch automatically the `ssh-agent`.
If the file doesn't exist, just create it.
This is what GitHub describes i... |
19602181 | How to extract one column of a csv file | 175 | 2013-10-26 02:32:40 | <p>If I have a csv file, is there a quick bash way to print out the contents of only any single column? It is safe to assume that each row has the same number of columns, but each column's content would have different length.</p>
| 280,715 | 788,171 | 2026-01-05 13:37:48 | 19,602,188 | 206 | 2013-10-26 02:34:38 | 210,613 | 2013-10-26 02:34:38 | https://stackoverflow.com/q/19602181 | https://stackoverflow.com/a/19602188 | <p>You could use awk for this. Change '$2' to the nth column you want.</p>
<pre><code>awk -F "\"*,\"*" '{print $2}' textfile.csv
</code></pre>
| <p>You could use awk for this. Change '$2' to the nth column you want.</p> <pre><code>awk -F "\"*,\"*" '{print $2}' textfile.csv </code></pre> | 73, 387, 5352 | bash, csv, extract | <h1>How to extract one column of a csv file</h1>
<p>If I have a csv file, is there a quick bash way to print out the contents of only any single column? It is safe to assume that each row has the same number of columns, but each column's content would have different length.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,012 | bash | # How to extract one column of a csv file
If I have a csv file, is there a quick bash way to print out the contents of only any single column? It is safe to assume that each row has the same number of columns, but each column's content would have different length. | You could use awk for this. Change '$2' to the nth column you want.
```
awk -F "\"*,\"*" '{print $2}' textfile.csv
``` |
13872048 | Bash Script : what does #!/bin/bash mean? | 164 | 2012-12-14 03:03:29 | <p>In bash script, what does <code>#!/bin/bash</code> at the 1st line mean ?</p>
<p><strong>UPDATE</strong>: Is there a difference between <code>#!/bin/bash</code> and <code>#!/bin/sh</code> ?</p>
| 447,333 | 188,331 | 2016-12-27 16:42:41 | 13,872,064 | 206 | 2012-12-14 03:04:29 | 1,741,864 | 2012-12-14 03:12:32 | https://stackoverflow.com/q/13872048 | https://stackoverflow.com/a/13872064 | <p>That is called a <a href="http://en.wikipedia.org/wiki/Shebang_%28Unix%29">shebang</a>, it tells the shell what program to interpret the script with, when executed.</p>
<p>In your example, the script is to be interpreted and run by the <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29">bash</a> shell.</p>... | <p>That is called a <a href="http://en.wikipedia.org/wiki/Shebang_%28Unix%29">shebang</a>, it tells the shell what program to interpret the script with, when executed.</p> <p>In your example, the script is to be interpreted and run by the <a href="http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29">bash</a> shell.</p>... | 387 | bash | <h1>Bash Script : what does #!/bin/bash mean?</h1>
<p>In bash script, what does <code>#!/bin/bash</code> at the 1st line mean ?</p>
<p><strong>UPDATE</strong>: Is there a difference between <code>#!/bin/bash</code> and <code>#!/bin/sh</code> ?</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,013 | bash | # Bash Script : what does #!/bin/bash mean?
In bash script, what does `#!/bin/bash` at the 1st line mean ?
**UPDATE**: Is there a difference between `#!/bin/bash` and `#!/bin/sh` ? | That is called a [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29), it tells the shell what program to interpret the script with, when executed.
In your example, the script is to be interpreted and run by the [bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) shell.
Some other example shebangs are:
... |
29776439 | Username and password in command for git push | 157 | 2015-04-21 15:20:07 | <p>It's possible to clone down a git repository, specifying username and password in the command. Example:</p>
<p><code>git clone https://username:password@myrepository.biz/file.git</code></p>
<p>Is it is possible to also specify the username and password when pushing? So that, for example, running <code>git push ori... | 366,302 | 3,987,179 | 2022-10-13 11:37:40 | 29,776,651 | 206 | 2015-04-21 15:28:40 | 29,489 | 2015-04-21 15:28:40 | https://stackoverflow.com/q/29776439 | https://stackoverflow.com/a/29776651 | <p>Yes, you can do</p>
<p><code>git push https://username:password@myrepository.biz/file.git --all</code></p>
<p>in this case <code>https://username:password@myrepository.biz/file.git</code> replace the <code>origin</code> in <code>git push origin --all</code></p>
<p>To see more options for <code>git push</code>, tr... | <p>Yes, you can do</p> <p><code>git push https://username:password@myrepository.biz/file.git --all</code></p> <p>in this case <code>https://username:password@myrepository.biz/file.git</code> replace the <code>origin</code> in <code>git push origin --all</code></p> <p>To see more options for <code>git push</code>, tr... | 119, 61874 | git, git-bash | <h1>Username and password in command for git push</h1>
<p>It's possible to clone down a git repository, specifying username and password in the command. Example:</p>
<p><code>git clone https://username:password@myrepository.biz/file.git</code></p>
<p>Is it is possible to also specify the username and password when pu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,014 | bash | # Username and password in command for git push
It's possible to clone down a git repository, specifying username and password in the command. Example:
`git clone https://username:password@myrepository.biz/file.git`
Is it is possible to also specify the username and password when pushing? So that, for example, runni... | Yes, you can do
`git push https://username:password@myrepository.biz/file.git --all`
in this case `https://username:password@myrepository.biz/file.git` replace the `origin` in `git push origin --all`
To see more options for `git push`, try `git help push` |
5227295 | How do I delete all lines in a file starting from after a matching line? | 126 | 2011-03-08 01:23:43 | <p>I have a file which is made up of several lines of text:</p>
<pre><code>The first line
The second line
The third line
The fourth line
</code></pre>
<p>I have a string which is one of the lines: <code>The second line</code></p>
<p>I want to delete the string and all lines after it in the file, so it will delete <c... | 133,791 | 500,281 | 2018-01-25 18:21:03 | 5,227,429 | 206 | 2011-03-08 01:46:43 | 26,428 | 2018-01-25 18:21:03 | https://stackoverflow.com/q/5227295 | https://stackoverflow.com/a/5227429 | <p>If you don't want to print the matched line (or any following lines):</p>
<pre><code>sed -n '/The second line/q;p' inputfile
</code></pre>
<p>This says "when you reach the line that matches the pattern quit, otherwise print each line". The <code>-n</code> option prevents implicit printing and the <code>p</code> co... | <p>If you don't want to print the matched line (or any following lines):</p> <pre><code>sed -n '/The second line/q;p' inputfile </code></pre> <p>This says "when you reach the line that matches the pattern quit, otherwise print each line". The <code>-n</code> option prevents implicit printing and the <code>p</code> co... | 58, 387, 5282 | bash, linux, sed | <h1>How do I delete all lines in a file starting from after a matching line?</h1>
<p>I have a file which is made up of several lines of text:</p>
<pre><code>The first line
The second line
The third line
The fourth line
</code></pre>
<p>I have a string which is one of the lines: <code>The second line</code></p>
<p>I ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,015 | bash | # How do I delete all lines in a file starting from after a matching line?
I have a file which is made up of several lines of text:
```
The first line
The second line
The third line
The fourth line
```
I have a string which is one of the lines: `The second line`
I want to delete the string and all lines after it in... | If you don't want to print the matched line (or any following lines):
```
sed -n '/The second line/q;p' inputfile
```
This says "when you reach the line that matches the pattern quit, otherwise print each line". The `-n` option prevents implicit printing and the `p` command is required to explicitly print lines.
or
... |
9953448 | How to remove all white spaces from a given text file? | 122 | 2012-03-31 06:03:00 | <p>I want to remove all the white spaces from a given text file. Is there any shell command available for this? Use <code>sed</code> perhaps?</p>
<p>I tried this : <code>cat hello.txt | sed 's/ //g'</code></p>
<p>But it removes only spaces, not tabs.</p>
| 457,828 | 986,020 | 2025-04-24 20:52:19 | 9,953,487 | 206 | 2012-03-31 06:11:21 | 444,036 | 2017-10-25 15:20:23 | https://stackoverflow.com/q/9953448 | https://stackoverflow.com/a/9953487 | <pre><code>$ man tr
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
Translate, squeeze, and/or delete characters from standard
input, writing to standard output.
</code></pre>
<p>In order to wipe all whitespace including newlines you can try:</p>
<pre><code>ca... | <pre><code>$ man tr NAME tr - translate or delete characters SYNOPSIS tr [OPTION]... SET1 [SET2] DESCRIPTION Translate, squeeze, and/or delete characters from standard input, writing to standard output. </code></pre> <p>In order to wipe all whitespace including newlines you can try:</p> <pre><code>ca... | 387, 5282 | bash, sed | <h1>How to remove all white spaces from a given text file?</h1>
<p>I want to remove all the white spaces from a given text file. Is there any shell command available for this? Use <code>sed</code> perhaps?</p>
<p>I tried this : <code>cat hello.txt | sed 's/ //g'</code></p>
<p>But it removes only spaces, not tabs.</p>
| q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,016 | bash | # How to remove all white spaces from a given text file?
I want to remove all the white spaces from a given text file. Is there any shell command available for this? Use `sed` perhaps?
I tried this : `cat hello.txt | sed 's/ //g'`
But it removes only spaces, not tabs. | ```
$ man tr
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
Translate, squeeze, and/or delete characters from standard
input, writing to standard output.
```
In order to wipe all whitespace including newlines you can try:
```
cat file.txt | tr -d " \t\n\r"
``... |
8653921 | How to give arguments to kill via pipe | 110 | 2011-12-28 09:14:43 | <p>I need to search for a certain process and kill that process. I wrote a command like this:</p>
<pre><code>ps -e | grep dmn | awk '{print $1}' | kill
</code></pre>
<p>Where the process name is <code>dmn</code>. But it is not working. How can I find processes by name and <code>kill</code> them.</p>
| 94,715 | 567,879 | 2023-10-30 14:23:55 | 8,653,966 | 206 | 2011-12-28 09:18:52 | 1,118,979 | 2023-10-30 14:23:55 | https://stackoverflow.com/q/8653921 | https://stackoverflow.com/a/8653966 | <pre><code>kill $(ps -e | awk '/dmn/ {print $1}')
</code></pre>
| <pre><code>kill $(ps -e | awk '/dmn/ {print $1}') </code></pre> | 58, 387, 390, 1231, 31134 | bash, command-line, command-line-arguments, linux, shell | <h1>How to give arguments to kill via pipe</h1>
<p>I need to search for a certain process and kill that process. I wrote a command like this:</p>
<pre><code>ps -e | grep dmn | awk '{print $1}' | kill
</code></pre>
<p>Where the process name is <code>dmn</code>. But it is not working. How can I find processes by name a... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,017 | bash | # How to give arguments to kill via pipe
I need to search for a certain process and kill that process. I wrote a command like this:
```
ps -e | grep dmn | awk '{print $1}' | kill
```
Where the process name is `dmn`. But it is not working. How can I find processes by name and `kill` them. | ```
kill $(ps -e | awk '/dmn/ {print $1}')
``` |
32232978 | Change the location of the ~ directory in a Windows install of Git Bash | 197 | 2015-08-26 17:27:34 | <p>I am not even sure I am asking the right question. Let me explain my situation:</p>
<p>This is about Git on Windows 7.</p>
<p>My company sets up the Windows user directory on a network drive, not on the local hard drive (for backup and other purposes beyond the scope of this question). I cannot change that po... | 239,285 | 4,830,015 | 2025-09-09 14:20:36 | 32,233,667 | 205 | 2015-08-26 18:06:30 | 971,141 | 2025-09-09 14:20:36 | https://stackoverflow.com/q/32232978 | https://stackoverflow.com/a/32233667 | <p>You should set the <code>$HOME</code> environment variable since that solves exactly what you're asking for.</p>
<p><code>cd ~</code> doesn't mean <em>change to the root directory</em>, but <strong>change to the user's home directory</strong>, which is set by the <code>$HOME</code> environment variable.</p>
<h2>Quic... | <p>You should set the <code>$HOME</code> environment variable since that solves exactly what you're asking for.</p> <p><code>cd ~</code> doesn't mean <em>change to the root directory</em>, but <strong>change to the user's home directory</strong>, which is set by the <code>$HOME</code> environment variable.</p> <h2>Quic... | 119, 21628, 27017, 61874, 105819 | git, git-bash, mingw32, msys2, msysgit | <h1>Change the location of the ~ directory in a Windows install of Git Bash</h1>
<p>I am not even sure I am asking the right question. Let me explain my situation:</p>
<p>This is about Git on Windows 7.</p>
<p>My company sets up the Windows user directory on a network drive, not on the local hard drive (for back... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,018 | bash | # Change the location of the ~ directory in a Windows install of Git Bash
I am not even sure I am asking the right question. Let me explain my situation:
This is about Git on Windows 7.
My company sets up the Windows user directory on a network drive, not on the local hard drive (for backup and other purposes beyond... | You should set the `$HOME` environment variable since that solves exactly what you're asking for.
`cd ~` doesn't mean *change to the root directory*, but **change to the user's home directory**, which is set by the `$HOME` environment variable.
## Quick'n'dirty solution
Edit `C:\Program Files (x86)\Git\etc\profile` ... |
3306007 | Replace a string in shell script using a variable | 146 | 2010-07-22 05:28:31 | <p>I am using the below code for replacing a string
inside a shell script.</p>
<pre><code>echo $LINE | sed -e 's/12345678/"$replace"/g'
</code></pre>
<p>but it's getting replaced with <code>$replace</code> instead of the value of that variable.</p>
<p>Could anybody tell what went wrong?</p>
| 657,308 | 134,713 | 2022-12-10 17:04:09 | 3,306,024 | 204 | 2010-07-22 05:31:34 | 14,860 | 2021-01-11 11:45:13 | https://stackoverflow.com/q/3306007 | https://stackoverflow.com/a/3306024 | <p>If you want to interpret <code>$replace</code>, you should not use single quotes since they prevent variable substitution.</p>
<p>Try:</p>
<pre><code>echo $LINE | sed -e "s/12345678/${replace}/g"
</code></pre>
<p>Transcript:</p>
<pre><code>pax> export replace=987654321
pax> echo X123456789X | sed &qu... | <p>If you want to interpret <code>$replace</code>, you should not use single quotes since they prevent variable substitution.</p> <p>Try:</p> <pre><code>echo $LINE | sed -e "s/12345678/${replace}/g" </code></pre> <p>Transcript:</p> <pre><code>pax> export replace=987654321 pax> echo X123456789X | sed &qu... | 34, 390, 5282 | sed, shell, unix | <h1>Replace a string in shell script using a variable</h1>
<p>I am using the below code for replacing a string
inside a shell script.</p>
<pre><code>echo $LINE | sed -e 's/12345678/"$replace"/g'
</code></pre>
<p>but it's getting replaced with <code>$replace</code> instead of the value of that variable.</p>
<p>Could ... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,019 | bash | # Replace a string in shell script using a variable
I am using the below code for replacing a string
inside a shell script.
```
echo $LINE | sed -e 's/12345678/"$replace"/g'
```
but it's getting replaced with `$replace` instead of the value of that variable.
Could anybody tell what went wrong? | If you want to interpret `$replace`, you should not use single quotes since they prevent variable substitution.
Try:
```
echo $LINE | sed -e "s/12345678/${replace}/g"
```
Transcript:
```
pax> export replace=987654321
pax> echo X123456789X | sed "s/123456789/${replace}/"
X987654321X
pax> _
```
Just be careful to en... |
18537098 | Spaces cause split in path with PowerShell | 141 | 2013-08-30 15:57:38 | <p>I'm having an issue with PowerShell when invoking an exe at a path containing spaces.</p>
<pre class="lang-none prettyprint-override"><code>cd C:\Windows Services
invoke-expression "C:\Windows Services\MyService.exe"`
</code></pre>
<p>Output:</p>
<blockquote>
<p>The term 'C:\Windows' is not recognized as t... | 323,208 | 306,098 | 2025-04-09 23:32:38 | 18,537,263 | 204 | 2013-08-30 16:08:01 | 2,157,862 | 2020-02-01 14:03:32 | https://stackoverflow.com/q/18537098 | https://stackoverflow.com/a/18537263 | <p>Would this do what you want?:</p>
<pre><code>& "C:\Windows Services\MyService.exe"
</code></pre>
<p>Use <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#call-operator-" rel="noreferrer"><code>&</code>, the call operator</a>, to i... | <p>Would this do what you want?:</p> <pre><code>& "C:\Windows Services\MyService.exe" </code></pre> <p>Use <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#call-operator-" rel="noreferrer"><code>&</code>, the call operator</a>, to i... | 526 | powershell | <h1>Spaces cause split in path with PowerShell</h1>
<p>I'm having an issue with PowerShell when invoking an exe at a path containing spaces.</p>
<pre class="lang-none prettyprint-override"><code>cd C:\Windows Services
invoke-expression "C:\Windows Services\MyService.exe"`
</code></pre>
<p>Output:</p>
<blockqu... | q.PostTypeId = 1; a.PostTypeId = 2; EXISTS tag '%bash%' OR '%shell%'; a.Body contains code block; q.Score > 5; a.Score > 10 | 1,020 | bash | # Spaces cause split in path with PowerShell
I'm having an issue with PowerShell when invoking an exe at a path containing spaces.
```
cd C:\Windows Services
invoke-expression "C:\Windows Services\MyService.exe"`
```
Output:
> The term 'C:\Windows' is not recognized as the name of a cmdlet, function, script file, o... | Would this do what you want?:
```
& "C:\Windows Services\MyService.exe"
```
Use [`&`, the call operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#call-operator-), to invoke commands whose names or paths are stored in quoted strings and/or ar... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.