314 votes
Accepted

How to append tee to a file in Bash?

echo -e "First Line" | tee ~/output.log echo -e "Second Line" | tee -a ~/output.log ^^ From man tee: Copy standard input to each FILE, and also to standard output. ...
user4556274's user avatar
  • 4,877
309 votes

Why does "(base)" appear in front of my terminal prompt?

This can also be because auto_activate_base is set to True. You can check this using the following command conda config --show | grep auto_activate_base To set it false conda config --set ...
bksahu's user avatar
  • 3,191
228 votes
Accepted

How do I show the git branch with colours in Bash prompt?

This snippet: # Add git branch if its present to PS1 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } if [ "$color_prompt" = yes ]; then PS1='${...
kos's user avatar
  • 35.9k
208 votes

How do I show the git branch with colours in Bash prompt?

Ubuntu: Show your branch name on your terminal Add these lines in your ~/.bashrc file # Show git branch name force_color_prompt=yes color_prompt=yes parse_git_branch() { git branch 2> /dev/null | ...
Sam's user avatar
  • 2,189
188 votes
Accepted

How to change Windows line-ending to Unix version

Option 1: dos2unix You can use the program dos2unix, which is specifically designed for this: dos2unix file.txt will replace all CR from all lines, in place operation. To save the output in a ...
heemayl's user avatar
  • 91.8k
176 votes

Make Command Not Found

Run this command to install make and all the packages needed to build your code. sudo apt-get install build-essential
JorelC's user avatar
  • 1,863
144 votes
Accepted

What is the meaning of exit 0, exit 1, and exit 2 in a bash script?

Here's one good reference for shell exit codes: Exit code 0 Success Exit code 1 General errors, Miscellaneous errors, such as "divide by zero" and other impermissible operations Exit ...
user535733's user avatar
  • 62.3k
132 votes
Accepted

What is the point of sh being linked to dash?

The short answer to “why the original sh shell isn't present in sh” is that there's no original sh. Well, ok, there is: it's the Thompson shell. Version 1 had some of the features we know today, in ...
Gilles 'SO- stop being evil''s user avatar
124 votes
Accepted

Why does "(base)" appear in front of my terminal prompt?

This appears to come from your conda environment. In particular, you are activating conda from your ~/.bashrc as follows # added by Anaconda3 installer #export PATH="/home/jim/anaconda3/bin:$PATH" . /...
steeldriver's user avatar
122 votes
Accepted

What does $# mean in bash?

$# is a special variable in bash, that expands to the number of arguments (positional parameters) i.e. $1, $2 ... passed to the script in question or the shell in case of argument directly passed to ...
heemayl's user avatar
  • 91.8k
121 votes

How to make LV use all disk space in PV?

I also used the default Ubuntu 20.04 install from ISO w/ lvm option selected. I had the same problem with the OS disk not occupying what I had allocated. Eddie's suggestion and the provided link did ...
cmd-not-found's user avatar
110 votes

How to pass an array as function argument?

If you want to pass one or more arguments AND an array, I propose this change to the script of @A.B. Array should be the last argument and only one array can be passed #!/bin/bash function copyFiles()...
SBF's user avatar
  • 1,201
109 votes
Accepted

What command is the alias ll for?

You can use the alias or type commands to check what a specific alias means: $ alias ll alias ll='ls -alF' $ type ll ll is aliased to `ls -alF' Note however that aliases might use other aliases, so ...
Byte Commander's user avatar
  • 108k
103 votes

How do I fix "$'\r': command not found" errors running Bash scripts in WSL?

Inside WSL: sudo apt-get install dos2unix Then, dos2unix [file] Full documentation: man dos2unix Saved my day, hope it helps.
Chaim Eliyah's user avatar
  • 1,169
101 votes

Script to display all terminal colors

A simple one-liner that should work for most people. msgcat --color=test
Jacek Trociński's user avatar
101 votes
Accepted

How to display modification time of a file?

Don't use ls, this is a job for stat: stat -c '%y' filename -c lets us to get specific output, here %y will get us the last modified time of the file in human readable format. To get time in seconds ...
heemayl's user avatar
  • 91.8k
98 votes
Accepted

Differentiate Interactive login and non-interactive non-login shell

The only real misconception you seem to have is about what constitutes a non-interactive, login shell. Briefly (see here for more details), with examples: interactive login shell: You log into a ...
terdon's user avatar
  • 101k
97 votes
Accepted

Why is there a /bin/echo and why would I want to use it?

If you open up a bash prompt and type in an echo command, that uses a shell builtin rather than running /bin/echo. The reasons it is still important for /bin/echo to exist are: You're not always ...
Eliah Kagan's user avatar
92 votes
Accepted

How can I sum numbers on lines in a file

bc with a little help from paste to get the lines in a single one with + as the separator: paste -sd+ file.txt | bc To use the output of grep (or any other command) instead a static file, pass the ...
heemayl's user avatar
  • 91.8k
89 votes
Accepted

Best way to read a config file in bash

As mbiber said, source another file. For example, your config file (say some.config) would be: var1=val1 var2=val2 And your script could look like: #! /bin/bash # Optionally, set default values # ...
muru's user avatar
  • 198k
87 votes
Accepted

What does grep -v "grep" mean and do?

grep -v "grep" takes input line by line, and outputs only the lines in which grep does not appear. Without -v, it would output only the lines in which grep does appear. See man grep for details. As ...
Eliah Kagan's user avatar
86 votes
Accepted

Why can't I cd to a quoted tilde ('~')?

As @karel noted in his answer, ~ is a special character and expanded by Bash into the current user's home directory. See Bash's manual on "Tilde Expansion", or search for the headline "Tilde Expansion"...
Byte Commander's user avatar
  • 108k
79 votes
Accepted

What does this syntax mean?

$ type echo echo is a shell builtin meaning, the echo command is part of the bash program itself (assuming you use bash) -n is an option, so let's see what it does $ help echo Write arguments to ...
Zanna's user avatar
  • 70.5k
73 votes
Accepted

Request root privilege from within a script

This'll work: echo "$(whoami)" [ "$UID" -eq 0 ] || exec sudo "$0" "$@" example: ./test.sh blade [sudo] password for blade: root
blade19899's user avatar
  • 26.7k
71 votes
Accepted

How can I run OpenVPN as root in the background, in a script?

TL;DR: Use sudo -b or, better, openvpn [...] --daemon. Since you're running openvpn (and, less specifically, since you wish to run a program as root in the background), the most commonly dispensed ...
Eliah Kagan's user avatar
68 votes
Accepted

How to check computer's timezone through Terminal?

Use: timedatectl status | grep "Time zone" Without the grep you get a lot more of information. As pointed out by @Christian Ulbrich in comment the wording for timezone seems to change depending on ...
Videonauth's user avatar
  • 33.4k
68 votes

Create alias for ssh connecting

Use the intended way and write the options and aliases into ~/.ssh/config: Host 1 Port 12345 User my_user HostName 123.123.123.1 Host 2 Port 12345 User my_user HostName 123.123.123.2 ...
Jakuje's user avatar
  • 6,615
64 votes
Accepted

Is there a way reverse of what mkdir -p option does?

The reversal of the mkdir -p command would be rmdir -p. rmdir -p will remove the folder structure up till the folder is not empty. You should be able to use rmdir instead of mkdir on your command: ...
Terrance's user avatar
  • 41.7k
63 votes
Accepted

Is it possible to stop .sudo_as_admin_successful being created?

Based on the following section of the plugins/sudoers/sudoers.c source code file, it doesn't look like it's possible without recompiling sudo, undefining the USE_ADMIN_FLAG pre-processor macro. Also, ...
steeldriver's user avatar
63 votes
Accepted

How do I split a "/proc/*/environ" file in separate lines?

The entries are separated by the null character, see man 5 proc: /proc/[pid]/environ This file contains the environment for the process. The entries are separated by null bytes ('\0'), ...
muru's user avatar
  • 198k

Only top scored, non community-wiki answers of a minimum length are eligible