Posts tagged tricks

Enable export of all environment variables in bash

When setting up environment variables, for example in .profile you can write the following near the top of the file to enable exporting of all the environment variables without explicitly writing “export” each time:

set -o allexport

with the above, then you can just write the following, for example:


instead of

export TEST_DIR=/tmp/test

To disable the all export:

set +o allexport

You are also able to set environment variables without the export or allexport, however that will NOT make the variable available to sub-processes.

Count bytes and characters with wc

Count bytes and characters with wc

wc -c filename == counts the bytes
wc -m filename == counts the characters

this can be combined with other commands like grep, awk, sed

example with sed:

sed -n '1p' filename | wc -c

the above will print the first line from filename and count the bytes in that line

Fix ^M in Linux/UNIX using vi/vim

Sometimes if you edit a file in windows and then open it in Linux/UNIX it will have the special characters ^M at the end of each sentence. To fix ^M in Linux/UNIX using vi/vim run the following:

%s/[ctrlkey+v and ctrl-key+M]//g

sed tricks to replace multi blank spaces and tabs with single space

Replace all multi-blankspaces (more than one spaces/tabs) in file with mono-blankspace (one space):

sed 's/[<space>][<tab>][<space>][<tab>]*/ /g' filename

can also be:

 sed 's/[<space>][<tab>][<space>][<tab>]*/ /g' filename > newfilename

note: g in the end means GLOBAL

In the above samples, <space> means ‘ ‘ (empty space) and <tab> means ‘      ‘ (tab key press).

Go to Top