Skip to main content

Questions tagged [bash]

Bash is a free shell for Unix-like operating systems from the GNU Project.

Filter by
Sorted by
Tagged with
798 votes
13 answers
855k views

What does 'source' do?

$ whatis source source: nothing appropriate. $ man source No manual entry for source $ source bash: source: filename argument required source: usage: source filename [arguments] It exists, and it is ...
Andrea Ambu's user avatar
  • 8,648
568 votes
6 answers
209k views

Difference between .bashrc and .bash_profile

What's the difference between .bashrc and .bash_profile and which one should I use?
cfischer's user avatar
  • 9,093
495 votes
11 answers
1.3m views

What is the home directory on Windows Subsystem for Linux?

When I start bash on Windows Subsystem for Linux, it drops me in the directory /mnt/c/Users/<username> When I ls this directory, I see the directories Desktop, Documents, etc. that I can see ...
bcf's user avatar
  • 5,093
489 votes
7 answers
260k views

What is the difference between executing a Bash script vs sourcing it?

What is the difference between executing a Bash script like A and sourcing a Bash script like B? A > ./myscript B > source myscript
Scottie T's user avatar
  • 5,091
461 votes
11 answers
557k views

How do I add text to the beginning of a file in Bash?

Hi I want to prepend text to a file. For example I want to add tasks to the beginning of a todo.txt file. I am aware of echo 'task goes here' >> todo.txt but that adds the line to the end of ...
user479534's user avatar
  • 4,821
433 votes
19 answers
699k views

How to encode base64 via command line in macOS/OS X?

Is there a terminal command in macOS/OS X which will base64 encode a file or stdin?
Josh's user avatar
  • 9,797
402 votes
21 answers
562k views

How do I detach a process from Terminal, entirely?

I use Tilda (drop-down terminal) on Ubuntu as my "command central" - pretty much the way others might use GNOME Do, Quicksilver or Launchy. However, I'm struggling with how to completely detach a ...
user avatar
397 votes
13 answers
287k views

How can I search the bash history and rerun a command?

Can I search history in bash and run the result?
Richard Hoskins's user avatar
390 votes
4 answers
680k views

Go back to previous directory in shell

Is there a way to go back to previous directory we were in using bash,tcsh without using pushd/popd ? I'd like to type something like "back" and got returned to the previous directory I was in. Edit: ...
Lydon Ch's user avatar
  • 6,029
387 votes
5 answers
511k views

Bash: Iterating over lines in a variable

How does one properly iterate over lines in bash either in a variable, or from the output of a command? Simply setting the IFS variable to a new line works for the output of a command but not when ...
Alex Spurling's user avatar
372 votes
10 answers
383k views

chown is not changing symbolic link

I'm trying to change the user/group of a symbolic link with the command: $ chown -h myuser:mygroup mysymbolic/ But it's not changing. I'm logged in as root. The current user/group is set to root:...
user avatar
258 votes
5 answers
469k views

Linux Bash Script, Single Command But Multiple Lines?

I have the following script I wrote by searching Google, and it backs up my Linux system to an archive: #!/bin/bash # init DATE=$(date +20%y%m%d) tar -cvpzf /share/Recovery/Snapshots/$HOSTNAME_$...
Jay LaCroix's user avatar
  • 2,601
249 votes
11 answers
371k views

What to do when Ctrl + C can't kill a process?

Ctrl + C doesn't always work to kill the current process (for instance, if that process is busy in certain network operations). In that case, you just see "^C" by your cursor and can't do ...
Dustin Boswell's user avatar
242 votes
6 answers
156k views

In bash, how do I escape an exclamation mark?

I want to do something like bzr commit -m "It works!". I can sort of escape the exclamation mark by doing bzr commit -m "It works\!". However, then my commit message includes the backslash. How do I ...
Matthew's user avatar
  • 14.8k
236 votes
3 answers
98k views

What does the 'rc' in `.bashrc`, etc. mean? [duplicate]

Well, this is embarrassing. I feel like I just realised I don't know a loyal colleague's name after working with them for 10 years. "Hey, er... *cough!*, thanks for that great job you did on setting ...
Ian Mackinnon's user avatar
225 votes
26 answers
292k views

Bash scripting: test for empty directory

I want to test if a directory doesn't contain any files. If so, I will skip some processing. I tried the following: if [ ./* == "./*" ]; then echo "No new file" exit 1 fi That gives the ...
Anthony Kong's user avatar
  • 5,068
225 votes
4 answers
57k views

Using watch with pipes

I'd like to run this command: watch -n 1 tail -n 200 log/site_dev.log | grep Doctrine But it does not run, because "I think" that the grep tries to run on the watch instead of the tail... Is there ...
Tommy B.'s user avatar
  • 2,599
225 votes
5 answers
225k views

How to ignore certain filenames using "find"?

One of my favorite BASH commands is: find . -name '*.*' -exec grep 'SearchString' {} /dev/null \; which searches the contents of all of the files at and below the current directory for the specified ...
Cody S's user avatar
  • 2,514
220 votes
7 answers
297k views

Why does Ctrl + V not paste in Bash (Linux shell)?

When I copy something to the clipboard and press Ctrl + V in Bash, nothing happens; However, right clicking and selecting Paste does the job. Why? Is there any reasonable issue (I'm sure there is) ...
Yasser Zamani's user avatar
218 votes
9 answers
312k views

How can I find only the executable files under a certain directory in Linux?

How can I find only the executable files under a certain directory in Linux?
HaiYuan Zhang's user avatar
206 votes
11 answers
41k views

I accidentally typed password into bash command line

I accidentally typed my password into bash command line, mistaking the Last login: ... line for Wrong password (I was in a hurry). What do I do to cover my trace? What I did was editing .bash_history ...
MaDa's user avatar
  • 2,013
203 votes
8 answers
216k views

Execute a command from another directory in bash

Say that I'm doing this: cd subdir git init cd ../ Is there a way to do this with a single command, or perhaps two, rather than having to move in and out of a directory in order to run a command ...
Trevor Burnham's user avatar
202 votes
2 answers
343k views

How to access Windows folders from Bash on Ubuntu on Windows

On the Bash on Ubuntu on Windows app, I only have root@localhost:~# ls -a .bash_history .bashrc .profile How do I access all of the Windows folders like Documents, Downloads, etc.?
Sergei Wallace's user avatar
198 votes
15 answers
216k views

Is it possible to `tail -f` the output of `dmesg`?

I want to do something like dmesg | tail -f but it doesn't work: I use Mac OS X v10.6.7 (Snow Leopard). By doing that, tail will exit, instead of monitoring the output. I wonder if there is a ...
Ivan Xiao's user avatar
  • 2,825
179 votes
12 answers
182k views

Remove a certain line from Bash history file

How can I remove a certain line from history's database?
w0rldart's user avatar
  • 2,993
179 votes
23 answers
67k views

Add directory to $PATH if it's not already there

Has anybody written a bash function to add a directory to $PATH only if it's not already there? I typically add to PATH using something like: export PATH=/usr/local/mysql/bin:$PATH If I construct ...
Doug Harris's user avatar
168 votes
4 answers
117k views

How to use spaces in a bash alias name?

I am trying to create an aliases in bash. What I want to do is map ls -la to ls -la | more In my .bashrc file this is what I attempted: alias 'ls -la'='ls -la | more' However it does not work ...
sixtyfootersdude's user avatar
165 votes
3 answers
50k views

Case insensitive tab completion in Bash

Is there any way to make Bash tab complete case insensitively? $ bash --version GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0) Copyright (C) 2007 Free Software Foundation, Inc. I am ...
Jeffrey Aylesworth's user avatar
163 votes
9 answers
527k views

looping through `ls` results in bash shell script

Does any one have a template shell script for doing something with ls for a list of directory names and looping through each one and doing something? I'm planning to do ls -1d */ to get the list of ...
Daniel A. White's user avatar
162 votes
10 answers
226k views

How can I do a recursive find and replace from the command line?

Using a shell like bash or zshell, how can I do a recursive 'find and replace'? In other words, I want to replace every occurrence of 'foo' with 'bar' in all files in this directory and its ...
Nathan Long's user avatar
  • 26.8k
156 votes
8 answers
258k views

Windows Linux Subsystem - Accessing Files outside of Ubuntu

Why, when I create a file from within Windows for example, can I not see it under the Linux sub system in Windows 10 (bash.exe) The screenshot should explain. Folder: OMG was created from within ...
Dave Hamilton's user avatar
152 votes
8 answers
139k views

How to configure PuTTY so that Home/End/PgUp/PgDn work properly in bash?

The keys Home, End, PageUp, PageDown all type a ~ in my bash session instead of moving the cursor / view around. Why does this happen and which settings do I need to change? GNU bash, version 4.0.28(...
RomanSt's user avatar
  • 9,817
151 votes
9 answers
240k views

How do I make Bash my default shell on Ubuntu?

I have a .bash_profile in my home directory, but it isn't getting run on login. If I do the following, then things seem to be as I expect: ssh myhost bash source ~/.bash_profile But normally that ...
James A. Rosen's user avatar
151 votes
3 answers
202k views

What does export do in BASH? [duplicate]

Possible Duplicate: Difference between “a=b” and “export a=b” in bash It is hard to admit, but I have never really understood what exactly export does to an environment ...
Chas. Owens's user avatar
  • 2,552
150 votes
4 answers
241k views

Display each sub-directory size in a list format using one line command in Bash?

I want to get a list of the directories and their sizes in a list format like how you get when you do a ls -l. The thing is that is there a one line command that can do this? I see others have long ...
Patoshi パトシ's user avatar
150 votes
4 answers
254k views

Bash if on single line

I would like to know how can I write if conditions inside a bash script on a single line. For example, how can I write this on a single line, and then put another one just like it on the next? if [ -...
Horațiu Mlendea's user avatar
149 votes
6 answers
258k views

How to prevent SSH from disconnecting if it's been idle for a while

I have a ssh connection to a machine which gets disconnected by that machine after 30 minutes of no user input. However, if I start something like top, the connection stays alive. Since this is a ...
Isaac's user avatar
  • 1,904
145 votes
8 answers
172k views

Where does $PATH get set in OS X 10.6 Snow Leopard?

I type echo $PATH on the command line and get /opt/local/bin:/opt/local/sbin:/Users/andrew/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/pear/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/...
Andrew's user avatar
  • 15.1k
144 votes
6 answers
66k views

Does bash have a hook that is run before executing a command?

In bash, can I arrange for a function to be executed just before running a command? There is $PROMPT_COMMAND, which is executed before showing a prompt, i.e., just after running a command. Bash's $...
140 votes
7 answers
301k views

Get home directory by username

I want to obtain home dir of any user with echo echo ~puchuu >> /home/puchuu But I cant use variable echo ~$USER >> ~puchuu echo `echo ~$USER` >> ~puchuu
puchu's user avatar
  • 1,922
140 votes
3 answers
178k views

Where is the C drive in the WSL? [duplicate]

Whenever I try to login to bash using the Windows Subsystem for Linux, I try to cd into C:\Users\, but all I get is directory not found. Where is the C drive for the Windows Linux subsystem? Is it ...
Jeeter's user avatar
  • 2,901
139 votes
8 answers
186k views

Show only current directory name (not full path) on bash prompt

The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every ...
agentofuser's user avatar
  • 7,437
137 votes
6 answers
49k views

How do I reload .inputrc?

Background I have heard that the readline module is reading ~/.inputrc and that is how it changes the behaviour of keystrokes under programs such as bash. Question How can I reload this after ...
Captain Lepton's user avatar
133 votes
8 answers
1.1m views

How can I resolve the error "cannot execute binary file"?

When I login using SSH, all I can see is this... -bash: /usr/bin/id: cannot execute binary file -bash: [: : integer expression expected I couldn't do anything in here. Commands such as halt, ...
superuser's user avatar
  • 4,117
132 votes
18 answers
55k views

Substitution in text file **without** regular expressions

I need to substitute some text inside a text file with a replacement. Usually I would do something like sed -i 's/text/replacement/g' path/to/the/file The problem is that both text and replacement ...
Andrea's user avatar
  • 1,525
131 votes
17 answers
179k views

How to delete all files in a directory except some?

I need to delete all files in a directory, but exclude some of them. For example, in a directory with the files a b c ... z, I need to delete all except for u and p. Is there an easy way to do this?
Ashot's user avatar
  • 1,449
131 votes
2 answers
122k views

How to add a mp3 handler to sox?

I want to play music in the terminal with sox. # install sox/lame sudo apt-get install sox sudo apt-get install lame # play music play music.mp3 # play FAIL formats: no handler for file extension `...
kev's user avatar
  • 12.9k
130 votes
7 answers
38k views

How can I save the current contents of less to a file?

If I've piped the results of a command to less and then decided that I want to save the contents to a file, is this possible? I've tried setting a mark a at the end of the buffer, and then returning ...
Jonathan Day's user avatar
  • 1,611
130 votes
3 answers
43k views

How do I unset or get rid of a bash function?

If you set or export an environment variable in bash, you can unset it. If you set an alias in bash, you can unalias it. But there doesn't seem to be an unfunction. Consider this (trivial) bash ...
quack quixote's user avatar
129 votes
5 answers
286k views

Finding files which contain a certain string using find (1) and grep (1)

find . -type f -print -exec cat {} \; | grep some string Command above doesn't print the file path. I'm using: Ubuntu, bash 4.
H.Rabiee's user avatar
  • 1,393

1
2 3 4 5
236