# The Non-Annoying Terminal Mini How-To & Fun with shopt

## tomchuk

I recently posted this to gentoo-user and to my suprise many people had absolutely no idea how to reproduce this error, let alone how to solve it. THis is an essential piece of BASH knowledge that everyone should know.

Broken Line Wraping in Bash

What I'm talking about is the annoying habit BASH has of not checking its window size inbetween commands. To demonstrate, fire up your favorite terminal, and launch a "full screen" app like mutt, nano or less - I'll do "man bash" as it's appropriate to this How-To. Now resize your window to a larger size and quit whatever app you are using. Now try to type a long string.

Instead of:

```
tomchuk@dualie $cat foo | grep bar | awk this | sed that | blah, blah, blah,

blah, blah, blah, blah
```

you end up with:

```
blah, blah, blah, blahoo | grep bar | awk this | sed that | blah, blah, blah,
```

The line wraps onto the same line. Not very helpful.

Now try this. Open a terminal as above, and before you do anything type:

```
shopt -s checkwinsize
```

Repeat the steps you took before to induce the weird line wrap. Notice anything different? Now that's how a terminal should behave! What checkwinsize does is force BASH to check the size of the window after the completion of every command - adjusting how large it thinks the terminal is.

To make this system-wide add it to /etc/profile as root:

```
echo -e 'if [ "$SHELL" = "/bin/bash" ]\nthen\nshopt -s checkwinsize\nfi' >> /etc/profile
```

or to make sure your user has this set, add it to ~/.bashrc

```
echo "shopt -s checkwinsize" >> ~/.bashrc
```

More fun with shopt

Alright now that you have your terminal performing at an acceptable level, it's time to play with shopt a little more.

To list the options you have set to 'on':

```
tomchuk@dualie $ shopt -s

checkwinsize    on

cmdhist         on

expand_aliases  on

extglob         on

interactive_comments    on

login_shell     on

progcomp        on

promptvars      on

sourcepath      on

```

"shopt -s foo" will set option "foo" to 'on'

To list the options you have set to 'off':

```

thomas@dualie $ shopt -u

cdable_vars     off

cdspell         off

checkhash       off

execfail        off

histreedit      off

histappend      off

histverify      off

hostcomplete    off

huponexit       off

lithist         off

mailwarn        off

no_empty_cmd_completion off

nocaseglob      off

nullglob        off

restricted_shell        off

shift_verbose   off

xpg_echo        off

```

"shopt -u foo" will set option "foo" to 'off'

Look at all those options we're not using! I'll cover a couple of the more interesting ones. They're all available in man bash.

cdspell

Ever type "cd /sur/src/linus" or "cd /vat/rmp/postage"? Now you can reinforce your horrible typing with a shell that corrects spelling errors in paths for you.

```

thomas@dualie $ cd /sur/src/linus

-bash: cd: /sur/src/linus: No such file or directory

thomas@dualie $ shopt -s cdspell

thomas@dualie $ cd /sur/src/linus

/usr/src/linux

thomas@dualie $ ls

COPYING        Makefile        arch     include  lib      security

CREDITS        README          crypto   init     mm       sound

Documentation  REPORTING-BUGS  drivers  ipc      net      usr

MAINTAINERS    System.map      fs       kernel   scripts  vmlinux

```

dotglob

Every want to grep through everything in a directory, or move everything including dotfiles somewhere?

```

thomas@dualie $ ls -lA

total 0

-rw-r--r--    1 thomas   users           0 Dec  6 13:17 .file

-rw-r--r--    1 thomas   users           0 Dec  6 13:17 file

thomas@dualie $ chmod -r *

thomas@dualie $ ls -lA

total 0

-rw-r--r--    1 thomas   users           0 Dec  6 13:17 .file

--w-------    1 thomas   users           0 Dec  6 13:17 file

thomas@dualie $ shopt -s dotglob 

thomas@dualie $ chmod -r *

thomas@dualie $ ls -lA

total 0

--w-------    1 thomas   users           0 Dec  6 13:17 .file

--w-------    1 thomas   users           0 Dec  6 13:17 file

```

This sure beats "chmod -r .* *" as ".*" includes "." which will remove write permissions from the current directory as well as all the dotfiles in it - not good.Last edited by tomchuk on Mon Dec 08, 2003 5:47 am; edited 4 times in total

----------

## genneth

 :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy:  :Very Happy: 

yes...!!!!

----------

## ikokai

Thanks, I always wondered how to get rid off this.

----------

## marshall_j

cheers for that. that always annoyed me!!

----------

## twiggy

yey.. that has been bugging me quite some time now, thanks   :Very Happy: 

----------

## castorilo

Thanks. I was always annoyed by this and had no clue on how to reproduce this or fix it.

----------

## neenee

thanks  :Wink: 

----------

## Unne

That's really great.  I always thought that bash quirk was just something I had to live with.

----------

## Jeedo

same here, thx.

----------

## Halanegri

 *Unne wrote:*   

> That's really great.  I always thought that bash quirk was just something I had to live with.

 

same here

----------

## X

Happy day.

----------

## verbatim

Very cool.  :Smile: 

----------

## Config

Wow, this is just awesome - thank you a lot!

Now I can come back and enjoy pine  :Smile: 

----------

## grzewho

this line should get to the official gentoo /etc/profile (dunno if it`s baselayout`s or bash`s file)

----------

## viperlin

thanks that bash thing annoyed the hell out of me   :Laughing: 

----------

## ikaro

bupping this up, Thanks !

gotta love these fixes   :Cool: 

----------

## jesterspet

 *tomchuk wrote:*   

> To make this system-wide add it to /etc/profile as root

 

Um.. shouldn't this be added to /etc/bashrc instead of /etc/profile  :Question: 

Since this is bash specific, and not relevant to other shells, it really doesn't belong in /etc/profile .

I cannot remember if /etc/bashrc is included in a default install, or if the check to see if it exists is included in /etc/skel/.bashrc (or ~/.bashrc) so just for the sake of completeness, I'll point out that copying & editing the one that checks for /etc/profile in ~/.bashrc works just fine.

----------

## tomchuk

Glad everyone is enjoying this  :Very Happy: 

 *jesterspet wrote:*   

> Um.. shouldn't this be added to /etc/bashrc instead of /etc/profile  

 

Good catch, thanks.

I agree it shouldn't be in /etc/profile as it was, but I don't believe that gentoo's bash install looks at /etc/bashrc - it's not mentioned in the man page and I couldn't make it happen, short of adding "source /etc/bashrc" to my ~/.bashrc which defeats the purpose. I added an if statement that checks $SHELL before issueing shopt which does the trick.

----------

## Krigare

this is just perfect  :Smile: 

----------

## tarzan420

I had gotten the impression that the word wrap problems had to do with ANSI colors in the bash profile (You know, green/red username, blue path).  I came to this conclusion after I read more about them in a recent GWN, and then tried to use the colors for my prompt on my debian box.  While the color worked, I still had strange line wrapping problems.  Cause and Effect? I'm not sure.

----------

## tomchuk

Like you said, weird line wraps can also be caused by improperly escaped prompts (PS1). For every non-printing character (color codes, etc) you must escape them with \[ and \].

A simple colored PS1:

```
PS1='\033[01;34m\u@\033[01;31m\h \$'
```

That will cause weird line wrapping because when calculating the window width and the length of the prompt bash considers the color codes as priniting characters. The solution is to escape the color codes like:

```
PS1='\[\033[01;34m\]\u@\[\033[01;31m\]\h \$'
```

Try each of those and you'll find that the first will wrap the line weirdly but the second is just fine.

----------

## OneOfOne

awesome!

----------

## rhill

bump.

----------

## SaFrOuT

although it is an old thread but it was very helpful

thanks dirtyepic for bumbing it 

and SURE thanks tomchuk

----------

## zsoltika

Hi guys,

I followed this steps:

 *tomchuk wrote:*   

> 
> 
> Broken Line Wraping in Bash
> 
> What I'm talking about is the annoying habit BASH has of not checking its window size inbetween commands. To demonstrate, fire up your favorite terminal, and launch a "full screen" app like mutt, nano or less - I'll do "man bash" as it's appropriate to this How-To. Now resize your window to a larger size and quit whatever app you are using. Now try to type a long string.
> ...

 

but after this I still end up whit messed up command line   :Sad: 

I mean, using this 

```
export PS1="[\e[31m\$(pwd)\e[0m :: \e[32m\@\e[0m :: \e[33m\l\e[0m]\n[\e[32m\u\e[

0m@\e[34m\H\e[0m] \$ "
```

 I still end up with command lines like this:

```
aaaaaaaaaaaaaaaaaaaaaaa'ss prova_a0.svg | grep 'aaaaaaaaaaaaaa
```

I'm using Xterm(196) and GNU bash, version 3.00.13(1)-release (i686-pc-linux-gnu).

Any idea?

Thx,

Zsoltika

----------

## rhill

probably bash 3.0

that thing is all kinds of crazy.

----------

## Mistobaan

Added to my .bashrc   :Cool: 

----------

## I1

 *tomchuk wrote:*   

> 
> 
> To make this system-wide add it to /etc/profile as root:
> 
> ```
> ...

 

Yes, that did it!!!

Thanks tomchuk, this really annoyed the heck out of me!

Shouldn't someone put this in the standard /etc/profile in sys-apps/baselayout? 

It took me quite a while to find this fix....

Thx,

Iwan.

----------

## Shemite_Dog

Excellent, this had been annoying me for a long time   :Very Happy:   :Cool: 

Speaking of annoying,  sometimes I get corrupted shell fonts after cat-ing a device driver. Everything turns into miscellaneous symbols. There is a way to restore things  (I read this in a post a long while back) but I cannot for the life of me find this old post anymore or remember what was the solution. Does anyone know how to fix this?

----------

## ahubu

 *Shemite_Dog wrote:*   

> 
> 
> Speaking of annoying,  sometimes I get corrupted shell fonts after cat-ing a device driver. Everything turns into miscellaneous symbols. (...) Does anyone know how to fix this?

 

Yes, you just have to type this command in the garbled term:

```
reset
```

You have to trust your own typing skills, because you can't see what you type.   :Wink: 

----------

## jmanning

My new favorite is CDPATH

```
export CDPATH=".:~:/:/usr/portage:/my/src/dir/"
```

Then, from anywhere:

```

$ cd etc

/etc

$ cd .ssh

~/.ssh

$ cd app-shells

/usr/portage/app-shells

```

It even does completion: cd <TAB>

----------

## ahubu

Wow CDPATH sure is mighty hefty, but confusing too... I am still dubbing whether I shall use it or not... this may introduce some serious human error increase... no more blindly directory-tab-completing  :Wink: 

----------

## thebigslide

 *ahubu wrote:*   

>  *Shemite_Dog wrote:*   
> 
> Speaking of annoying,  sometimes I get corrupted shell fonts after cat-ing a device driver. Everything turns into miscellaneous symbols. (...) Does anyone know how to fix this? 
> 
> Yes, you just have to type this command in the garbled term:
> ...

 

Holy smokes.  I've been rebooting to fix this for years  :Very Happy: 

----------

## keykeen

Hi,i follow the step,

the bash can check the window size,

but the line still wraps in one line.

even if i start shell without run any app.

----------

## Sir No

 *ahubu wrote:*   

> Yes, you just have to type this command in the garbled term:
> 
> ```
> reset
> ```
> ...

 

But if you happen to use a Linux installation where there's no reset command... Then you can try this:

```
echo -ne "\017"
```

Read the Linux Gazette issue 5, issue 6 and issue 7 to get a wider picture.

----------

## bravecobra

Excellent post. Tnx

----------

## raccoon

 *thebigslide wrote:*   

> Holy smokes.  I've been rebooting to fix this for years :D

 

Hah, I second that!

----------

## zxy

Great post!  :Smile: 

----------

