Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 2421

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.). Check scripts with https://shellcheck.net before posting.

74 votes

What is the meaning of $? in a shell script?

$? expands to the exit status of the most recently executed foreground pipeline. See the Special Parameters section of the Bash manual. In simpler terms, it's the exit status of the last command.
cjm's user avatar
  • 27.7k
143 votes
Accepted

Find the owner of a directory or file, but only return that and nothing else

stat from GNU coreutils can do this: stat -c '%U' /path/of/file/or/directory Unfortunately, there are a number of versions of stat, and there's not a lot of consistency in their syntax. For examp …
cjm's user avatar
  • 27.7k
29 votes
Accepted

How to print out only the script name?

Use basename: #!/bin/bash basename -- "$0" If you want to assign it to a variable, you'd do: my_name=$(basename -- "$0")
cjm's user avatar
  • 27.7k
9 votes
Accepted

Bash CD up until in certain folder

This has to be a shell function, not a script, because a script executes in a new shell (and thus can't change the directory of the original shell). function cdroot() { while [[ $PWD != '/' && ${PW …
cjm's user avatar
  • 27.7k
5 votes
Accepted

Background script run at login interrupted when I run tmux in terminal

As explained in 6.2 Bash Startup Files, ~/.profile is executed when you start an interactive login shell (or use the --login option). Since tmux's default behavior is to start a login shell in each n …
cjm's user avatar
  • 27.7k
1 vote

Sourcing a shell script from within Emacs

If all you need is to unset LD_LIBRARY_PATH after loading Emacs, just add (setenv "LD_LIBRARY_PATH") to ~/.emacs or other init file. If you need to swap back and forth, save the result of (getenv …
cjm's user avatar
  • 27.7k
8 votes

aliasing cd to pushd - is it a good idea?

This isn't a direct answer to the question, but I fell in love with the directory history window in 4DOS. So much so that I wrote my own version for Linux (and Cygwin). I've never gotten around to m …
cjm's user avatar
  • 27.7k
3 votes

Switching to superuser while shell script is running

su starts a new shell (or runs some other command). It doesn't magically switch the UID of the current shell. If you type exit after entering your password, you'll see that your script continues wit …
cjm's user avatar
  • 27.7k