tools:bash:customization
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tools:bash:customization [2024/05/19 21:47] โ created - external edit 127.0.0.1 | tools:bash:customization [2025/01/04 03:40] (current) โ Humphrey Boa-Gart | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | {{wst> | ||
// This article is **Part 6** in a [[tools: | // This article is **Part 6** in a [[tools: | ||
====== Bash: Customization Basics ====== | ====== Bash: Customization Basics ====== | ||
- | 29,14,'## | + | The real power of bash is in its potential for limitless customization. When a bash shell is started, it goes down a list of predefined paths, looking for customization files. Depending on your operating system, it is likely that 99% of what you will need to do can be accomplished in **.bash_aliases**, |
+ | |||
+ | **.bashrc** and **.bash_profile** will take edits as well, but this is generally not advisable. Some operating systems have things mixed around, but by default most of them will have comments in the headings of these files explaining how they are arranged on your machine. | ||
+ | |||
+ | ===== Invocation Methods | ||
+ | |||
+ | Depending on how bash is invoked and initialized, | ||
+ | |||
+ | ==== Invoked as interactive login shell ==== | ||
+ | |||
+ | (ex: SSH, SU) | ||
+ | |||
+ | - **/ | ||
+ | - **/ | ||
+ | - **/etc/bashrc** //(more global settings)// | ||
+ | - **~/ | ||
+ | - **~/.bashrc** //(more user configuration)// | ||
+ | - **~/.bash_aliases** //(user aliases file)// | ||
+ | - **~/ | ||
+ | |||
+ | ==== Invoked as interactive non-login shell ==== | ||
+ | |||
+ | (ex: within X window manager) | ||
+ | |||
+ | | ||
+ | - **~/ | ||
+ | |||
+ | ==== Invoked non-interactively | ||
+ | |||
+ | (ex: scripts) | ||
+ | |||
+ | | ||
+ | |||
+ | ===== Aliases ===== | ||
+ | |||
+ | As you start to use bash more often, you will find yourself using specific combinations of commands and flags to accomplish basic tasks. You can drastically cut the amount of typing you will do by setting **aliases**, | ||
+ | |||
+ | For example, lets look at the '' | ||
+ | |||
+ | ls='ls -lhAF --color=auto' | ||
+ | |||
+ | Aliases do not have to be permanent, either. To set that same alias to only work for the current shell session, you would run: | ||
+ | |||
+ | $ alias ls=\'ls -lhAF --color=auto\' | ||
+ | |||
+ | To unset that alias, you would run: | ||
+ | |||
+ | | ||
+ | |||
+ | You can also unset all aliases | ||
+ | |||
+ | $ unalias -a | ||
+ | |||
+ | To see a list of active aliases, run: | ||
+ | |||
+ | $ alias -p | ||
+ | |||
+ | ===== More Simple Tricks ===== | ||
+ | |||
+ | You can set all sorts of things up in your aliases file. The following are just a few tweaks you can make: | ||
+ | |||
+ | ==== Custom Hostnames ==== | ||
+ | |||
+ | You can tweak the prompt, aka the string that appears before $ in your terminal. (Usually something like **[user@host]$**) | ||
+ | |||
+ | If you want to restyle the prompt, you will need to set it to the **$PS1** variable. Here is one example with custom green and blue color codes, which you can copy/paste to your aliases file and use right away: | ||
+ | |||
+ | PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01; | ||
+ | |||
+ | Here is a separate <wrap em>red version</ | ||
+ | |||
+ | PS1='\[\033[01;31m\]\u@\h\[\033[00m\] \[\033[01; | ||
+ | |||
+ | Be careful setting the **$PS1** variable, as you can very easily make your shell unusable if you set it the wrong way! | ||
+ | |||
+ | //(See 'ANSI Escape Codes' at the bottom of this page for more color options.)// | ||
+ | |||
+ | ==== Harden umask ==== | ||
+ | |||
+ | By default your **umask** is probably set up to give read access to things that do not need it by default. To make it so all new files/ | ||
+ | |||
+ | umask 0077 | ||
+ | |||
+ | //(See [[tools: | ||
+ | |||
+ | ===== Environment Variables | ||
+ | |||
+ | Just as in any programming language, bash uses **variables**. A variable in bash can contain a number, a character, or a string of characters. | ||
+ | |||
+ | You have no need to declare a variable, as just assigning a value to its reference will create it. | ||
+ | |||
+ | ==== $PATH ==== | ||
+ | |||
+ | The most important variable you will deal with, is the **$PATH** variable, which specifies the directories that bash will look for executable files. (So you can type '' | ||
+ | |||
+ | $ echo $PATH | ||
+ | |||
+ | This will output something like: | ||
+ | |||
+ | / | ||
+ | |||
+ | This above line shows that bash will look in your personal **~/ | ||
+ | |||
+ | To enable bash to run a custom application (such as a freshly downloaded [[tools: | ||
+ | |||
+ | $ ln -s / | ||
+ | |||
+ | With '' | ||
+ | |||
+ | You could also put the file or symlink in **/ | ||
+ | |||
+ | ==== Getting Variable Info ==== | ||
+ | |||
+ | See list of global variables: | ||
+ | |||
+ | | ||
+ | |||
+ | Same as above, but with less available option flags: | ||
+ | |||
+ | | ||
+ | |||
+ | See full list of global and local variables: | ||
+ | |||
+ | | ||
+ | |||
+ | Another | ||
+ | |||
+ | | ||
+ | |||
+ | See value for global variable HOME: | ||
+ | |||
+ | | ||
+ | |||
+ | Same as above: | ||
+ | |||
+ | | ||
+ | |||
+ | Use global variable HOME as part of another command: | ||
+ | |||
+ | $ ls $HOME | ||
+ | |||
+ | ==== Setting Variables ==== | ||
+ | |||
+ | Set value of new local variable \' | ||
+ | |||
+ | | ||
+ | |||
+ | Same as above, but for strings: | ||
+ | |||
+ | | ||
+ | |||
+ | Append | ||
+ | |||
+ | | ||
+ | |||
+ | Assign | ||
+ | |||
+ | | ||
+ | |||
+ | Export | ||
+ | |||
+ | | ||
+ | |||
+ | Removes | ||
+ | |||
+ | $ unset testvar | ||
+ | |||
+ | ==== Variable Arrays | ||
+ | |||
+ | Define multiple values for variable as array: | ||
+ | |||
+ | | ||
+ | |||
+ | Echo column three from vararray: | ||
+ | |||
+ | | ||
+ | |||
+ | Echo entire array from vararray: | ||
+ | |||
+ | | ||
+ | |||
+ | Change | ||
+ | |||
+ | | ||
+ | |||
+ | Remove | ||
+ | |||
+ | $ unset vararry[2] | ||
+ | |||
+ | ==== Variable Notes ==== | ||
+ | |||
+ | * Get value of variables with `$`, but don\'t use `$` when assigning them. | ||
+ | * Global environment variables can be accessed from any child shells of the parent from which it was defined. | ||
+ | * Local environment variables can only be accessed from the process in which they were defined. | ||
+ | * Export and unset, when used in a child shell, will not affect the parent shell. | ||
+ | * Variable arrays start with an index value of zero, not one. | ||
+ | |||
+ | ===== ANSI Escape Codes ===== | ||
+ | |||
+ | When you set your custom hostname earlier in this article, you may have noticed the **\033[..m]** or **\033[..; | ||
+ | |||
+ | ==== Color Codes ==== | ||
+ | |||
+ | ^ Color ^ Foreground Code ^ Background Code ^ | ||
+ | | Black | 30 | 40 | | ||
+ | | Red | 31 | 41 | | ||
+ | | Green | 32 | 42 | | ||
+ | | Yellow | ||
+ | | Blue | 34 | 44 | | ||
+ | | Magenta | ||
+ | | Cyan | 36 | 46 | | ||
+ | | Light Gray | 37 | 47 | | ||
+ | | Gray | 90 | 100 | | ||
+ | | Light Red | 91 | 101 | | ||
+ | | Light Green | 92 | 102 | | ||
+ | | Light Yellow | 93 | 103 | | ||
+ | | Light Blue | 94 | 104 | | ||
+ | | Light Magenta | 95 | 105 | | ||
+ | | Light Cyan | 96 | 106 | | ||
+ | | White | 97 | 107 | | ||
+ | |||
+ | ==== Other Codes ==== | ||
+ | |||
+ | ^ Code ^ Description | ||
+ | | 00 | Reset/ | ||
+ | | 01 | Bold text | | ||
+ | | 02 | Faint text | | ||
+ | | 03 | Italics | ||
+ | | 04 | Underlined text | | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | * Previous Page: [[tools: | ||
+ | * [[tools: | ||
+ | |||
+ | |||
+ | {{wst> |
tools/bash/customization.1716155256.txt.gz ยท Last modified: 2024/08/06 05:54 (external edit)
Find this page online at: https://bestpoint.institute/tools/bash/customization
Find this page online at: https://bestpoint.institute/tools/bash/customization