Linux.com

Feature

CLI Magic: Enhancing the shell with fish

By Bruce Byfield on November 13, 2006 (8:00:00 AM)

Share    Print    Comments   

The Friendly Interactive Shell (fish) is an alternative command line that is designed to be easy to learn and use. fish turns on by default options that are available in shells such as Bash or tcsh and develops them far beyond other shells. The result is a command line that can go a long way toward curing the phobia that many GNU/Linux users nurse from their experience with the DOS command line.

The fish shell has far too many features to discuss all of them in detail. The four that users are most likely to encounter first and appreciate most are the browser help, advanced syntax highlighting, tab completion, and command history.

Fish comes with the usual man pages, as well as help files available with a simple -h switch. However, unlike most shells, fish also comes with an HTML help page. Users can access the top of the help page by typing help. They can enter a bookmark name after the command (for example, help variables) or a fish-specific command to jump to a particular section. This arrangement is somewhat weakened by the fact that, until they read the help for the first time, users who lack shell experience may not know what sections exist. Yet, in general, fish makes good use of browser help by presenting information in easy-to-scan bulleted lists.

Advanced syntax highlighting

By default, fish enables the coloring of different file types in the ls command, but that is only the beginning of fish's syntax highlighting. As you move about your home directory, fish adds subdirectories to the prompt in green. Move out of your own directory, and paths are added from the root directory of the system. As you enter commands, fish colors each aspect of them, displaying commands in green, paths and files in the default shell color, and regular expressions in blue. Similarly, when the cursor is at one quotation mark or parenthesis, its matching one is also highlighted. These color conventions are handy for newcomers who are still learning, but experienced users who know them may find the highlighting equally useful for keeping track of complex syntax.

fish displays in red a wide variety of errors, ranging from non-existent files and commands to unclosed parentheses and incorrect redirects of output. This display of errors can be useful, but it can also be a distraction; since fish reinterprets your input as you type, it sometimes displays an error simply because you haven't finished typing yet. The coloring of errors can be turned off in the configuration files, but a more useful option might be to suppress the display of a string as an error until it is complete.

Tab completion and command history

In the default Bash shell that comes with most GNU/Linux distributions, you can type a string and press the Tab key to have a command or file name automatically completed, but only if you type enough of the command that only one completion is available. The feature saves some typing, but is not very useful if you fail to remember the exact spelling of the command.

By contrast, fish boasts what its wiki describes as "tab completion as steroids." In fish, tab completion can be called at any point in your typing, not just when a single alternative is possible. The results are displayed in a scrollable pager with the text you entered highlighted, and a two- or three-word description of each result. You can then highlight a result and paste it into the cursor.

In addition to saving time and aiding users' memories, tab completion in fish also serves as a quick search utility. For example, instead of using the find command, you could enter py then press Tab to see what Python-related files are installed on your system, or enter apt on a Debian system to see what apt-based utilities are installed. Although its capacity is more limited than that of search tools, fish's tab completion has the advantage of requiring less expertise to use effectively.

Like tab completion, command history is streamlined in fish, with all but the most recent of duplicate entries automatically removed from the history. This removal is desirable because fish allows you to search the history. Normally, pressing the Up or Down arrow keys scrolls through the history just as in Bash, but if a string is entered at the prompt first, fish displays only entries in the history that match the string. You can also refine the search even further by pressing Alt-Up or Alt-Down to divide the string into separate elements and search only for the element the cursor is currently on. If a search through the history takes too long, you can stop it by pressing the Esc key.

Advanced features, faults, and assessment

These are only the most obvious features of fish. More experienced users may appreciate its regularized command syntax and English-like scripting, or its specialized environment variables to set such elements as the default browser and new file permissions, or the ability to customize features such as syntax highlighting and tab completion. Some of the commands specific to fish, such as open, which reads the desktop environment's MIME database to determine what program to open a file in, or fg and bg, which together toggle processes out of and into the foreground, offer further small extensions of standard shell functionality. When you switch to other shells, some of these features are sufficiently different in structure that they might be confusing, but, regarded by themselves, they represent a more rational and powerful shell than many users are likely to have seen.

The fish shell does have some faults. Some of its messages are unnecessarily cryptic; failing to specify a destination with the cp command, for example, results in the unhelpful message "Unexpected end of token" -- a phrase far more unhelpful than anything I've ever seen in Bash. On my Debian testing installation, fish terminals also showed a tendency to randomly crash.

Yet, despite these shortcomings, fish remains an interesting example of what a shell can be. Anyone who has scanned the man pages of the ls or cp commands knows that the basic commands in a Bash shell are frequently more flexible and powerful than the desktop tools meant to replace them. By adding dozens of small but useful innovations, fish shows that, even after several decades of development, the standard Unix shell still has room for enhancements.

Bruce Byfield is a course designer and instructor, and a computer journalist who writes regularly for NewsForge, Linux.com and IT Manager's Journal.

Bruce Byfield is a computer journalist who writes regularly for Linux.com.

Share    Print    Comments   

Comments

on CLI Magic: Enhancing the shell with fish

Note: Comments are owned by the poster. We are not responsible for their content.

Wikipedia article

Posted by: Anonymous Coward on November 13, 2006 08:00 PM
Wikipedia has an article about 'fish';
* <a href="http://en.wikipedia.org/wiki/Friendly_interactive_shell" title="wikipedia.org">http://en.wikipedia.org/wiki/Friendly_interactive<nobr>_<wbr></nobr> shell</a wikipedia.org>

Don't forget to checkout the official website though;
* <a href="http://fishshell.org/" title="fishshell.org">http://fishshell.org/</a fishshell.org>

#

Re:Wikipedia article

Posted by: Anonymous Coward on November 16, 2006 01:32 PM
I'm sure there are many articles on Fish out there. Why the hell do we care about Wikipedia's?

#

Good article

Posted by: Anonymous Coward on November 13, 2006 11:57 PM
A good article, I have heard about the 'fish' shell before, but I have never tried it before. It was a good article, it makes me want to try out this 'fish' shell.

I don't know so much about different shells, but I do like 'bash', because its nice and pretty simple yet still good.

Has anyone tried the 'rc' shell from Plan 9 from Bell Labs? There exists a Linux port for it too.
* <a href="http://en.wikipedia.org/wiki/Rc" title="wikipedia.org">http://en.wikipedia.org/wiki/Rc</a wikipedia.org>
Is it any good?

#

Re:Good article

Posted by: Anonymous Coward on November 14, 2006 02:00 AM
rc is fricking sweet. beautifully simple and consistent syntax.
fish is pretty good in that respect aswell (a LOT less cruft compared to bash), though it still has more complicated sh-style quoting rules and special character escapes (eg \u3081 for unicode).

But since unix has no decent terminal, rc is a real pain to use (you can maintain sanity by running rc in 9term from <a href="http://swtch.com/plan9port/" title="swtch.com">plan9port</a swtch.com>, though you won't be able to run a lot of native unix apps - 9term doesn't understand your escape codes).
And that is most of the reason fish is more complicated than rc - fish has to make up for the deficiencies of unix terminals and provide features like line editing/filename completion (in rc's native habitat these are provided by the terminal so the shell doesn't have to worry about it).

-sqweek

#

Erroneous

Posted by: Anonymous Coward on November 14, 2006 03:18 AM
This article contains so many errors it's not even funny. Bash has most (all?) of the features mentioned in this article; you can easily search the command history using Ctrl-R. Pressing Tab twice brings up the same list that fish supposedly presents ("py", Tab, Tab, for instance).

Are cp and ls build-in commands in fish? cp and ls are, in most environments, plain binaries. The author seems to believe that cp and ls are built-in commands in bash -- they are certainly not.

#

Re:Erroneous

Posted by: Anonymous Coward on November 16, 2006 11:48 PM
I think you are exagerating the faults of the article. Bash most certainly does not have 'most (all?) of the features mentioned in this article'. Specifically, bash does not have an integrated help system, bash does not do syntax highlighting, bash does not give descriptions of its completions, bash does not have a regularized syntax. Bash can page the completions, but unlike fish, you can't move freely in the list, nor is the list resized to fit your screen. Bash can search the history, bash has job control, etc. but it is clear to me from the article that the author means that fish provides smaller enhancements to the abilities of bash in these areas.

As to whether the author thinks ls and cp are builtins, it is clear to me from the wording in the article ('fish enables the coloring of different file types in the ls command') that the author knows that ls is not a builtin. I would guess that the bit about cp was just careless writing.

#

commands can be contained in Fish

Posted by: Administrator on November 14, 2006 05:02 PM

Have you seen BusyBox? It contains its commands directly in itself. <a href="http://www.busybox.net/downloads/BusyBox.html" title="busybox.net">Currently available functions</a busybox.net> include:



[, [[, addgroup, adduser, adjtimex, ar, arping, ash, awk,
basename, bbconfig, bunzip2, busybox, bzcat, cal, cat, catv,
chattr, chgrp, chmod, chown, chroot, chvt, cksum, clear, cmp,
comm, cp, cpio, crond, crontab, cut, date, dc, dd, deallocvt,
delgroup, deluser, devfsd, df, diff, dirname, dmesg, dnsd,
dos2unix, dpkg, dpkg_deb, du, dumpkmap, dumpleases, e2fsck, echo,
ed, eject, env, ether_wake, expr, fakeidentd, false, fbset,
fdflush, fdformat, fdisk, find, fold, free, freeramdisk, fsck,
fsck_minix, ftpget, ftpput, fuser, getopt, getty, grep, gunzip,
gzip, halt, hdparm, head, hexdump, hostid, hostname, httpd,
hwclock, id, ifconfig, ifdown, ifup, inetd, init, insmod,
install, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, iproute,
iptunnel, kill, killall, klogd, lash, last, length, less, ln,
loadfont, loadkmap, logger, login, logname, logread, losetup, ls,
lsattr, lsmod, lzmacat, makedevs, md5sum, mdev, mesg, mkdir,
mke2fs, mkfifo, mkfs_minix, mknod, mkswap, mktemp, modprobe,
more, mount, mountpoint, mt, mv, nameif, nc, netstat, nice,
nohup, nslookup, od, openvt, passwd, patch, pidof, ping, ping6,
pivot_root, poweroff, printenv, printf, ps, pwd, rdate, readlink,
readprofile, realpath, reboot, renice, reset, rm, rmdir, rmmod,
route, rpm, rpm2cpio, run_parts, runlevel, rx, sed, seq, setarch,
setconsole, setkeycodes, setlogcons, setsid, sha1sum, sleep,
sort, start_stop_daemon, stat, strings, stty, su, sulogin, sum,
swapoff, swapon, switch_root, sync, sysctl, syslogd, tail, tar,
tee, telnet, telnetd, test, tftp, time, top, touch, tr,
traceroute, true, tty, tune2fs, udhcpc, udhcpd, umount, uname,
uncompress, uniq, unix2dos, unlzma, unzip, uptime, usleep,
uudecode, uuencode, vconfig, vi, vlock, watch, watchdog, wc,
wget, which, who, whoami, xargs, yes, zcat, zcip

#

Re:Interesting...

Posted by: Anonymous Coward on November 16, 2006 11:51 PM
Fish is free software. You can download the source code from <a href="http://fishshell.org/" title="fishshell.org">http://fishshell.org/</a fishshell.org>. If you are running Fedora, Ubuntu or Debian unstable, fish should be installable through your standard package manager.

#

Interesting...

Posted by: Administrator on November 14, 2006 11:54 PM
This sounds very interesting. Can you tell me more about the features of Fish? Is this already out in the market of Asia? I would like to try it out for myself. Thank you. Hope to get a reply.

#

fish is now my default shell and I'm happy

Posted by: Administrator on November 16, 2006 08:52 AM
I'm still a newbie to Linux and this seems a major improvement on bash to my inexpert eyes.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya