Archive

Archive for the ‘bash’ Category

shopt: change additional shell optional behavior

November 22, 2014 1 comment

There is a command called shopt that allows you to change additional shell optional behavior.

I use both Ubuntu and Manjaro and I noticed that sometimes bash behaved differently on Manjaro. For instance, I had a folder called “Test_me”. Once accidentally I wrote “cd test_me” and Manjaro entered the folder “Test_me” (notice the capital ‘T’) without any problem. Under Ubuntu it was impossible :)

As it turned out, the different behaviour is due to different shopt settings. In the example above, “cdspell” was on in Manjaro, while in Ubuntu it was off by default.

Since I use both systems and I want bash to behave similarly, I added these lines to the end of my ~/.bashrc:

# shopt settings (normalize Ubuntu and Manjaro)
shopt -s cdspell
shopt -s dotglob
shopt -s hostcomplete
shopt -s nocaseglob
shopt -u sourcepath

I switched “sourcepath” off. Read this post if you want to know why.

Categories: bash, manjaro, ubuntu Tags: ,

the command “source” doesn’t exactly work the way you thought

November 22, 2014 1 comment

We know that “source” is a bash shell built-in command that executes the content of the file passed as argument in the current shell. It has a synonym: ‘.‘ (dot).

The classical use case is “source .bashrc” in your HOME directory. You do it when you modify your ~/.bashrc file and you want to activate the changes in the current terminal.

TIL something new. I had a discussion about it at reddit and the user geirha gave such a clear answer that I would quote him:

“The . (source is an alternative name for . in bash) command behaves more or less the same as when executing a command.

In POSIX sh, the following two will search for a file named foo in PATH (only).

    foo
    . foo

while the following two will specifically run and source, respectively, the file named foo in the current directory (only)

    ./foo
    . ./foo

Bash differs slightly (when not running in posix mode) in that when you give . or source an argument without any slash (/) characters (e.g. source foo), it searches through the PATH variable, like POSIX sh, but then also checks the current directory, if it was not found in PATH.” (end of quote)

Problem
Because of this I ran into a strange problem. I had a file called “.venv” that I wanted to source (source .venv) but I got some error. If I renamed the file, it worked well. But I wanted to call it “.venv”. What’s wrong?

Explanation
As explained above, it turned out that “source” started to look for “.venv” in the PATH first. Somehow it found a different file with the very same name and that caused the problem. When I renamed the file to “not_hidden” or “.jabba”, it worked.

Solution #1
The trivial way is to use “./” by telling source to take the argument from the current directory:

source ./.venv

However, in my entire life I thought that “source .bashrc” takes the file .bashrc from the current directory. It should, in my opinion :)

Solution #2
If you also think that “source” shouldn’t do anything with the PATH variable, there is a cure. This feature can be switched off. The command “shopt” is a builtin that allows you to change additional shell optional behavior.

The guilty option is this one:

sourcepath

    If set, the source builtin uses the value of PATH to find 
    the directory containing the file supplied as an argument. 
    This option is enabled by default.

Simply run “shopt” to see if “sourcepath” is on for you. You can switch it off easily:

shopt -u sourcepath

To make it permanent, add this line to the end of your ~/.bashrc.

After all this ado, I could finally write this:

. .venv

And this is the beauty of Linux. You learn something new every day :)

Links

Categories: bash Tags: , , , ,

colorize man pages

November 19, 2014 1 comment

Before
before

After
after

But how?

sudo apt-get install most

Then add this line to your ~/.bashrc:

export PAGER=most

Open a new terminal and try it.

Explanation
This tip is from here. As the user cannedprimates states, “you can solve this issue by using a different pager, for example most. man will actually use the program specified in the PAGER environment variable.”

Categories: bash Tags: , , , ,

Google Nexus device in the filesystem

November 7, 2014 3 comments

Problem
I got a Google Nexus tablet that I can manage with Nautilus (Ubuntu) / Thunar (Manjaro). However, it would be nice to manage this device with Midnight Commander too.

Solution
If the graphical file manager shows the address “mtp://[usb:007,005]/” for instance, then you will find it in the file system at “/run/user/1000/gvfs/mtp:host=%5Busb%3A007%2C005%5D“.

However, if you connect the device to a different USB port, the address will be different from “mtp://[usb:007,005]/“.

To enter the root directory of the device, I have the following script:

#!/usr/bin/env bash

cd /run/user/1000/gvfs/
cd `ls`
cd "Belső tárhely"

How to call it in the command line: “. nexus“. The script is called “nexus” in my case. Notice the “.” in front of it, which is an abbreviation for the command “source“.

Categories: bash Tags: , , ,

Launch gedit in the background

September 11, 2014 1 comment

Problem
Sometimes I use gedit but it is launched in the foreground, blocking my terminal. There are two ways to put it in the background: (1) Press CTRL+Z in the terminal, then send the job to the background with the command bg, or (2) launch gedit directly in the background with the “&” sign.

Neither of them is convenient. I often forget about it and I always end up in a blocked terminal. I just simply want to write “gedit” or “gedit ehh.txt“, and I want it to start in the background.

Solution
Create a script called “gedit” in your $HOME/bin directory with the following content:

/usr/bin/gedit "$@" &

Make it executable and make sure the directory $HOME/bin is in your PATH.

Categories: bash, ubuntu Tags: ,

Dr. Strangelove or: How I Learned to Stop Worrying and Love Bash

July 30, 2014 1 comment

In this post, Steve shows how to include Python code snippets in Bash scripts! Do you hate Bash? Me too :) However, with this trick bash becomes much less painful.

For more info, please refer to Steve’s post. Here I write a short summary for future references.

Example 1:

#!/bin/bash

function current_datetime {
python - <<END
import datetime
print datetime.datetime.now()
END
}

# Call it
current_datetime

# Call it and capture the output
DT=$(current_datetime)
echo Current date and time: $DT

Example 2 (passing parameters to the wrapping function):


#!/bin/bash

function line {
PYTHON_ARG="$1" python - <<END
import os
line_len = int(os.environ['PYTHON_ARG'])
print '-' * line_len
END
}

# Do it one way
line 80

echo 'Handy'

# Do it another way
echo $(line 80)

You can find another example with JSON at Steve’s blog.

Categories: bash, python Tags:
Follow

Get every new post delivered to your Inbox.

Join 73 other followers