Home > Articles > Apple > Operating Systems
␡
Page 1 of 7Next >
I want to make some simple aliases for my terminal shell and I keep getting this. Alias rgrep='find. Ctrl-d is exit, ctrl-l is clear and there are many more. And technical aspects of the iPhone, iPod, iPad, and Mac platforms.
Learn how to customize your OS X environment, including the shell prompt, shortcuts, installation of new software, and vi, by changing your configuration files, environment variables, and tbe path.
This chapter is from the book
UNIX for Mac OS X: Visual QuickPro Guide
This chapter is from the bookThis chapter is from the book
Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration files contain settings and commands that determine how the programs will behave—for instance, the files can modify the list of places your shell looks for the commands you enter (that list is called your PATH).
You change your Unix environment to have it more closely match your personal preferences and to shape it more closely to the way you work.
Examples of configuring your Unix environment include:
The first program to configure is your shell, since your shell is the primary program you use to interact with Unix. We will also show you how to configure the vi editor by editing a configuration file it uses (see Chapter 6, 'Editing and Printing Files,' to learn how to use vi).
It should come as no surprise by now that you configure your shell by editing text files.
Finding Configuration Files
User-configurable Unix programs (including your shell, the vi editor, and others) look for configuration files in your home directory when they start up. Most of the commands you have learned so far are not user-config-urable; neither the ls nor the cd command uses configuration files, for example. They do accept options on the command line but do not read any configuration files when you run them.
Many configuration filenames begin with a dot (.), so they are called dot files (use ls -a to see them). Often the filenames end in 'rc' (for resource). For example, the main config-uration file for the tcsh shell is ~/.tcshrc, a configuration file for the bash shell is ~/.bash_profile, and the configuration file for the vi editor is called ~/.exrc (ex is an older editor, and vi provides a 'visual interface' for it). There are actually several config-uration files available for each shell, and Table 7.1 lists the more common ones. In this chapter, we will concentrate on the ones you would change in the course of normal use. These files each have settings and commands for the particular program being configured. (Remember that ~ [the tilde] is a synonym for your home directory.)
Configuration files for shells are actually scripts. This means they are a series of commands written in the scripting language for the corresponding shell. They make use of variables, if-then conditions, and other scripting elements such as loops. (See Chapter 9, 'Creating and Using Scripts,' for more on scripts.)
Table 7.1 A Summary of Common Shells
Am I Configuring the Terminal Application or My Shell?
There's an important distinction to understand here.
The Terminal application you are using to access the command line in Mac OS X is not the same as your shell.
Terminal is a regular Mac OS X graphical application, like your Web browser or word processor. When you open a new window in Terminal, the application runs the appropriate Unix shell (determined by the Terminal application's preferences). Terminal is the program that is handling the screen display and keyboard input for the shell. When you type something in Terminal, the Terminal application passes that to the shell, and when the shell produces output, Terminal draws it on your screen.
The subtle point here is that there are actually other ways besides Terminal in which you can use your shell. One example: You can connect to your Mac using the command line over a network from another machine, which we'll cover in Chapter 10, 'Interacting with Other Unix Machines.' So when we tell you in this chapter that a change you make will take effect 'in the next Terminal window you open,' that is really a shorthand way of saying that the change will take effect in the next instance of your shell that you run, and that the easiest way to see it is to open a new Terminal window.
Related Resources
< BackPage 5 of 7Next >
UNIX for Mac OS X: Visual QuickPro Guide
This chapter is from the bookThis chapter is from the book Shell Aliases
Shell aliases are shortcut names for commands. Each alias consists of one word (or even one letter) that you can use instead of a longer command line. For example, you may find yourself using the command ls -F a lot. You can easily make a shortcut for that command: lf, for example. So when you use lf where the shell expects a command, then the shell will substitute ls -F.
To see all your current aliases:
You can create aliases at the command line or by adding them to a configuration file.
Aliases created at the command line are only in effect for as long as you use that shell—that is, they disappear when you close that Terminal window. If you want an alias to always be available, you must put it in a con-figuration file.
To create an alias in tcsh or csh:
To create an alias in tcsh (or csh) that is set every time you start a shell:
To create an alias in bash:
To create an alias in bash that is set every time you start a shell:
Shell functions
Unlike aliases in the tcsh shell, aliases in bash cannot have command-line arguments included in them. However, bash allows you to create shell functions, which can make use of their arguments.
The term shell function applies to series of shell command lines. This is similar to an alias, except that a shell function can be many lines long, and you may use the special variables $1 for the first argument, $2 for the second, and so on.
Shell functions should be defined in your ~/.bash_profile.
To create a shell function in bash:
Related Resources
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |