View previous topic :: View next topic |
Author |
Message |
tomchuk Guru

Joined: 23 Mar 2003 Posts: 317 Location: Brooklyn, NY
|
Posted: Fri Dec 05, 2003 5:46 pm Post subject: The Non-Annoying Terminal Mini How-To & Fun with shopt |
|
|
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:
Code: | tomchuk@dualie $cat foo | grep bar | awk this | sed that | blah, blah, blah,
blah, blah, blah, blah |
you end up with:
Code: | 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:
Code: | 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:
Code: | 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
Code: | 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':
Code: | 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':
Code: |
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.
Code: |
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?
Code: |
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 |
|
Back to top |
|
 |
genneth Apprentice


Joined: 24 Mar 2003 Posts: 152 Location: UK
|
Posted: Fri Dec 05, 2003 6:02 pm Post subject: |
|
|
          
yes...!!!! |
|
Back to top |
|
 |
ikokai n00b


Joined: 10 Nov 2003 Posts: 14
|
Posted: Fri Dec 05, 2003 6:10 pm Post subject: |
|
|
Thanks, I always wondered how to get rid off this. |
|
Back to top |
|
 |
marshall_j Tux's lil' helper


Joined: 22 Jan 2003 Posts: 98 Location: NZ
|
Posted: Fri Dec 05, 2003 9:38 pm Post subject: |
|
|
cheers for that. that always annoyed me!! |
|
Back to top |
|
 |
twiggy n00b


Joined: 25 Nov 2003 Posts: 65 Location: Sweden
|
Posted: Fri Dec 05, 2003 10:04 pm Post subject: |
|
|
yey.. that has been bugging me quite some time now, thanks  |
|
Back to top |
|
 |
castorilo Apprentice


Joined: 25 Dec 2002 Posts: 157
|
Posted: Fri Dec 05, 2003 10:27 pm Post subject: |
|
|
Thanks. I was always annoyed by this and had no clue on how to reproduce this or fix it. |
|
Back to top |
|
 |
neenee Veteran


Joined: 20 Jul 2003 Posts: 1786
|
Posted: Fri Dec 05, 2003 11:40 pm Post subject: |
|
|
thanks  |
|
Back to top |
|
 |
Unne l33t


Joined: 21 Jul 2003 Posts: 616
|
Posted: Sat Dec 06, 2003 2:00 am Post subject: |
|
|
That's really great. I always thought that bash quirk was just something I had to live with. |
|
Back to top |
|
 |
Jeedo Apprentice


Joined: 02 May 2003 Posts: 202 Location: Akureyri, Iceland
|
Posted: Sat Dec 06, 2003 2:03 am Post subject: |
|
|
same here, thx. |
|
Back to top |
|
 |
Halanegri Guru

Joined: 03 Mar 2003 Posts: 351 Location: Reykjavík, Iceland
|
Posted: Sat Dec 06, 2003 2:27 am Post subject: |
|
|
Unne wrote: | That's really great. I always thought that bash quirk was just something I had to live with. |
same here |
|
Back to top |
|
 |
X Apprentice


Joined: 30 Apr 2002 Posts: 192 Location: Lexington KY
|
Posted: Sat Dec 06, 2003 3:23 am Post subject: |
|
|
Happy day. |
|
Back to top |
|
 |
verbatim Apprentice

Joined: 13 Mar 2003 Posts: 223
|
Posted: Sat Dec 06, 2003 5:37 am Post subject: |
|
|
Very cool.  |
|
Back to top |
|
 |
Config Retired Dev


Joined: 25 May 2003 Posts: 187 Location: Zurich, Switzerland
|
Posted: Sat Dec 06, 2003 1:44 pm Post subject: |
|
|
Wow, this is just awesome - thank you a lot!
Now I can come back and enjoy pine  _________________ Config - caught by a chronic disease called tuxmania.... |
|
Back to top |
|
 |
grzewho l33t


Joined: 31 Dec 2002 Posts: 626 Location: /home/g
|
Posted: Sat Dec 06, 2003 5:29 pm Post subject: |
|
|
this line should get to the official gentoo /etc/profile (dunno if it`s baselayout`s or bash`s file) _________________
Code: | USE="freedom -software_patents" emerge --deep --update world |
|
|
Back to top |
|
 |
viperlin Veteran

Joined: 15 Apr 2003 Posts: 1319 Location: UK
|
Posted: Sat Dec 06, 2003 8:30 pm Post subject: |
|
|
thanks that bash thing annoyed the hell out of me  |
|
Back to top |
|
 |
ikaro Advocate


Joined: 14 Jul 2003 Posts: 2527 Location: Denmark
|
Posted: Mon Dec 08, 2003 3:49 am Post subject: |
|
|
bupping this up, Thanks !
gotta love these fixes  _________________ linux: #232767 |
|
Back to top |
|
 |
jesterspet Apprentice


Joined: 05 Feb 2003 Posts: 215 Location: Atlanta
|
Posted: Mon Dec 08, 2003 4:42 am Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho |
|
|
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
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. _________________ (X) Yes! I am a brain damaged lemur on crack, and would like to buy your software package for $499.95 |
|
Back to top |
|
 |
tomchuk Guru

Joined: 23 Mar 2003 Posts: 317 Location: Brooklyn, NY
|
Posted: Mon Dec 08, 2003 6:06 am Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho |
|
|
Glad everyone is enjoying this
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. |
|
Back to top |
|
 |
Krigare Tux's lil' helper


Joined: 12 Nov 2003 Posts: 92 Location: ::1
|
Posted: Mon Dec 08, 2003 9:06 am Post subject: |
|
|
this is just perfect  _________________ Together we are strong. |
|
Back to top |
|
 |
tarzan420 Tux's lil' helper

Joined: 05 Jul 2003 Posts: 81 Location: Fairbanks AK
|
Posted: Wed Dec 10, 2003 5:58 pm Post subject: Screwy Word Wrap because of ANSI Colors? |
|
|
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. |
|
Back to top |
|
 |
tomchuk Guru

Joined: 23 Mar 2003 Posts: 317 Location: Brooklyn, NY
|
Posted: Wed Dec 10, 2003 8:00 pm Post subject: |
|
|
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:
Code: | 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:
Code: | 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. |
|
Back to top |
|
 |
OneOfOne Guru


Joined: 28 May 2003 Posts: 368
|
Posted: Sat Dec 13, 2003 7:47 pm Post subject: |
|
|
awesome! |
|
Back to top |
|
 |
rhill Retired Dev


Joined: 22 Oct 2004 Posts: 1629 Location: sk.ca
|
Posted: Sun Nov 28, 2004 1:05 pm Post subject: |
|
|
bump. _________________ by design, by neglect
for a fact or just for effect |
|
Back to top |
|
 |
SaFrOuT Apprentice


Joined: 08 Jul 2003 Posts: 256 Location: Egypt
|
Posted: Sun Nov 28, 2004 2:47 pm Post subject: |
|
|
although it is an old thread but it was very helpful
thanks dirtyepic for bumbing it
and SURE thanks tomchuk |
|
Back to top |
|
 |
zsoltika l33t


Joined: 13 Nov 2003 Posts: 634 Location: Budapest, Hungary
|
Posted: Mon Nov 29, 2004 6:17 am Post subject: Re: The Non-Annoying Terminal Mini How-To & Fun with sho |
|
|
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.
Instead of:
Code: | tomchuk@dualie $cat foo | grep bar | awk this | sed that | blah, blah, blah,
blah, blah, blah, blah |
you end up with:
Code: | 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:
Code: | 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:
Code: | 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
Code: | echo "shopt -s checkwinsize" >> ~/.bashrc |
|
but after this I still end up whit messed up command line
I mean, using this Code: | 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: Code: | 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 |
|
Back to top |
|
 |
|