Tag: Bash

How to disable “did you mean…” feature in Linux shell

How to disable offers to install a package with an incorrectly entered command

In case the command you entered into the Linux console is not found, some distributions show additional information suggesting which packages the entered command might be in and how it can be installed.

An example of such a message:

Command 'magick' not found, did you mean:
  command 'magic' from deb magic
  command 'magics' from deb magics++
Try: sudo apt install <deb name>

Distributions that use this are for example Kali Linux and Ubuntu.

If for some reason you do not like this behavior of the terminal, and you want the error output to be limited to the message “command not found”, then this article will tell you how to do it.

Since the “did you mean…” feature is provided by the “command-not-found” package, you can remove the package to disable it:

sudo apt remove command-not-found

After that, restart your computer for the changes to take effect.

Solution for no errors appear after removing command-not-found

In order for the “command not found” messages to start showing again after removing command-not-found, restart your computer.

Disabling “did you mean…” feature without removing “command-not-found” package

This method can be used by a non-root user, that is, without the ability to remove packages.

Open file

  • .bashrc (for Bash shell)
  • .zshrc (for Zsh shell)

See also: How to find out which shell is in use in Linux

And add the line there

unset command_not_found_handle

Before adding the specified line to one of the shell files, check for lines that enable command-not-found. For example, in Kali Linux, the following lines are present:

# enable command-not-found if installed
if [ -f /etc/zsh_command_not_found ]; then
    . /etc/zsh_command_not_found
fi

Delete or comment out these lines in the .zshrc file to get:

## enable command-not-found if installed
#if [ -f /etc/zsh_command_not_found ]; then
#    . /etc/zsh_command_not_found
#fi

As a result, the command-not-found feature will be disabled in all newly opened terminals.

How to convert a string to lowercase in Bash

This note will show you how to convert a string to lowercase (small letters) on the Linux command line.

To convert a string to lower case regardless of its current case, use one of the following commands.

tr

echo "Hi all" | tr '[:upper:]' '[:lower:]'
hi all

Attention! If you want to change the case of any letters other than Latin (national alphabets, letters with diacritics), then do not use tr, but use any other solution suggested below. This is because the classic Unix tr operates on single-byte characters and is not compatible with Unicode.

AWK

echo "Hi all" | awk '{print tolower($0)}'
hi all

Bash

a="Hi all"
echo "${a,,}"
hi all

Starting with Bash 5.1, there is a conversion option “L”, which is intended to convert a string to lowercase:

${var@L}

Example:

v="heLLo"
echo "${v@L}"
hello

sed

echo "Hi all" | sed -e 's/\(.*\)/\L\1/'
hi all

Or this solution:

echo "Hi all" | sed -e 's/\(.*\)/\L\1/' <<< "$a"
hi all

Another solution:

echo "Hi all" | sed 's/./\L&/g'

Perl

echo "Hi all" | perl -ne 'print lc'
hi all

Python

a="Hi all"
b=`echo "print ('$a'.lower())" | python`; echo $b

Ruby

a="Hi all"
b=`echo "print '$a'.downcase" | ruby`; echo $b

PHP

b=`php -r "print strtolower('$a');"`; echo $b

NodeJS

b=`node -p "\"$a\".toLowerCase()"`; echo $b

In zsh

a="Hi all"
echo $a:l

How to convert a string to uppercase in Bash

This note will show you how to convert a string to upper case (capital letters, uppercase) on the Linux command line.

To convert a string to capital letters regardless of its current case, use one of the following commands.

tr

echo "Hi all" | tr '[:lower:]' '[:upper:]'
HI ALL

Attention! If you want to change the case of any letters other than Latin (national alphabets, letters with diacritics), then do not use tr, but use any other solution suggested below. This is because the classic Unix tr operates on single-byte characters and is not compatible with Unicode.

AWK

echo "Hi all" | awk '{print toupper($0)}'
HI ALL

Bash

a="Hi all"
echo "${a^^}"
HI ALL

Starting with Bash 5.1, there is a conversion option U, which is intended to convert a string to uppercase:

${var@U}

Example:

v="heLLo"
echo "${v@U}"
HELLO

sed

echo "Hi all" | sed -e 's/\(.*\)/\U\1/'
HI ALL

Or this solution:

echo "Hi all" | sed -e 's/\(.*\)/\U\1/' <<< "$a"
HI ALL

Another solution:

echo "Hi all" | sed 's/./\U&/g'

Perl

echo "Hi all" | perl -ne 'print uc'
HI ALL

Python

a="Hi all"
b=`echo "print ('$a'.upper())" | python`; echo $b

Ruby

a="Hi all"
b=`echo "print '$a'.upcase" | ruby`; echo $b

PHP

b=`php -r "print strtoupper('$a');"`; echo $b

NodeJS

b=`node -p "\"$a\".toUpperCase()"`; echo $b

In zsh

a="Hi all"
echo $a:u

How to find out which shell is in use in Linux

Linux shells

There are several Linux shells, each with its own characteristics. Examples of popular shells are Bash (which is the default shell on many distributions) and ZSH (which is the default shell on Kali Linux, it has many fans).

The shell information is contained in the $SHELL environment variable to see its value and to find out which shell is in use, run the command:

echo $SHELL

See what exactly will be displayed:

  • /bin/bash - means you have Bash
  • /usr/bin/zsh - means you have ZSH

How to find out which Linux shell is actually being used

But is it really that simple with shells?

Take a look at the following screenshot and try to guess which shell will handle subsequent commands?

The screenshot clearly shows that the command printed “/usr/bin/zsh”, so we can say that it is using the ZSH shell.

But this is the wrong answer.

Yes, the default shell on this system is ZSH, but just above the command is

bash

and even though the decoration or the command line prompt hasn't changed, Bash now handles the commands you enter.

This is easy to prove if you run the command

help

then Bash will show you a quick reference for its commands. And ZSH will display a message that the command was not found:

What does the $SHELL variable actually show? Apparently, this is just the default shell on this system, without taking into account the fact that the user could start another shell. However, outputting the value of the $SHELL variable will in most cases answer the question of which shell is being used on that system.

To verify that you are using Bash, you can run the command:

help

Another way to “reveal” Bash is to look at how the Ctrl+x Ctrl+v keystrokes respond. In response, Bash will print a line similar to the following:

GNU bash, версия 5.1.4(1)-release (x86_64-pc-linux-gnu)

Related: How to change the login shell in Linux. chsh instruction

How to run small Python code in Bash

Bash is not only and even not so much shell built-in functions as command line programs (utilities). By running these commands and transferring the received data to the pipeline (through the pipe), you can automate a variety of things, programming of which in other programming languages can take a lot of effort.

This post will show you how to run Python code in Bash, and how to run a Python program in a Bash script.

How to execute Python code in Bash

On the Bash command line, use the following construct to execute code:

python -c "CODE"

For instance:

python -c "print (\"It works\")"

Another option that may come in handy in more exotic circumstances:

bash -c 'python -c "print (\"CODE\")"'

For instance:

bash -c 'python -c "print (\"It works\")"'

How to run a Python program in a Bash script

To execute a Python program in a Bash script, use a command like:

python SCRIPT.py ARGUMENT1 ARGUMENT2 ARGUMENT3

An example of running the extractor.py script passing two arguments: the value of the $line variable and 8080:

python extractor.py $line 8080

How to run a Python program in a Bash script and assign its output to a variable

If you need to run a Python script, and then assign the program output to a Bash variable, then use a construction like this:

ПЕРЕМЕННАЯ=`python SCRIPT.py ARGUMENT1 ARGUMENT2 ARGUMENT3`

Example:

response=`python extractor.py $line 8080 2>/dev/null | sed -E "s/\/\/.+:/\/\/$line:/"`

How to embed Python code into a Bash script

If Python code cannot be used on one line and you do not want to use an external script to call, then the following construction will work for you:

#!/bin/bash

script=`cat <<_EOF_
CODE
_EOF_`

echo "$script" | python

Where the CODE is, paste the Python code.

This example is working:

#!/bin/bash

value="I will be in the script!"

script=`cat <<_EOF_
print ("It works")
print ("Line 2")
print ("Line 3: $value")
_EOF_`

echo "$script" | python

Another variant of this construction:

python << END
... CODE ...
END

And another option, in which the output is assigned as the value of the ABC variable:

ABC=$(python << END ... END)
Loading...
X