- Make an Alias in Bash or Zsh Shell in macOS with Terminal
- Question: Q: Using aliases in Terminal
- All replies
- 30 Handy Bash Shell Aliases For Linux / Unix / MacOS
- More about bash shell aliases
- How to list bash aliases
- How to define or create a bash shell alias
- How to disable a bash alias temporarily
- How to delete/remove a bash alias
- How to make bash shell aliases permanent
- A note about privileged access
- A note about os specific aliases
- 30 bash shell aliases examples
- #1: Control ls command output
- #2: Control cd command behavior
- #3: Control grep command output
- #4: Start calculator with math support
- #4: Generate sha1 digest
- #5: Create parent directories on demand
- #6: Colorize diff output
- #7: Make mount command output pretty and human readable format
- #8: Command short cuts to save time
- #9: Create a new set of commands
- #10: Set vim as default
- #11: Control output of networking tool called ping
- #12: Show open ports
- #13: Wakeup sleeping servers
- #14: Control firewall (iptables) output
- #15: Debug web server / cdn problems with curl
- #16: Add safety nets
- #17: Update Debian Linux server
- #18: Update RHEL / CentOS / Fedora Linux server
- #19: Tune sudo and su
- #20: Pass halt/reboot via sudo
- #21: Control web servers
- #22: Alias into our backup stuff
- #23: Desktop specific – play avi/mp3 files on demand
- #24: Set default interfaces for sys admin related commands
- #25: Get system memory, cpu usage, and gpu memory info quickly
- #26: Control Home Router
- #27 Resume wget by default
- #28 Use different browser for testing website
- #29: A note about ssh alias
- #30: It’s your turn to share…
- Conclusion
- Conclusion
Make an Alias in Bash or Zsh Shell in macOS with Terminal
To make aliases of macOS Unix commands in your bash or zsh shell on macOS and earlier versions, it is done via your .bash_profile or .zshrc file which lives in your home account directory, if the file does not already exist, just create one.
Launch Terminal from the /Application/Utilities folder
Go to your home directory by just entering ‘cd’ followed by the ‘return’ key to enter the command:
List your home directory contents including invisible files to see if the file already exists, use:
Create the .bash_profile or .zshrc file using the command line program called ‘nano’ if it doesn’t exist, if it does exist you add your aliases to the end of the file. The commands below assume you are in your home directory:
If you are not in your home directory you would use a prefix of the tilde ‘
’ and forward slash, which is the home directory path:
When the .bash_profile or .zshrc file is created you are ready to enter your alias commands.
So here I am using the alias ‘l’ to alias the command ‘ls -lah’
In nano ‘control+o’ to write the file out and ‘control+x’ to exit the file.
Refresh the shell environment by entering the command below:
That’s it, now the alias will take effect.
To add other aliases just start a new line, and apply the same formatting.
Источник
Question: Q: Using aliases in Terminal
I’m trying to add new alias to my terminal so I used the below commands
First I went to my bash_profile by using the commands
nano .bash_profile
Then I added the below line at the end of the file and saved it & exit the file
alias htdocs= «cd /Applications/mamp/htdocs»
After that I closed the terminal and opened it again to restart it, then I typed «htdocs» then pressed enter but still found the terminal informing me «-sh: htdocs: command not found «
Now I’m asking why the alias not working with me . , what I miss .
MacBook Pro (Retina, 13-inch, Mid 2014), OS X Mavericks (10.9.5)
Posted on Feb 28, 2018 5:46 AM
All replies
Loading page content
Page content loaded
It has been years since I used MAMP, so I don’t know if your path to htdocs is correct. Isn’t MAMP capitalized?
When you update the
/.bashrc file, you either update your Bash session by quitting the Terminal and restarting it, or with the following command which updates your alias without quitting Terminal:
Also, I use single, straight quotes on my alias target.
Feb 28, 2018 6:13 AM
After I used the command source
/.bash_profile , I found it working well now
Feb 28, 2018 6:23 AM
That would imply that you did not quit Terminal and re-launch it after modifying the .bash_profile
Of that you have some funky Terminal preferences set that is preventing .bash_profile from being read by the shell when you start.
I’m assuming you are running the default shell ‘bash’
If you are running a different shell, that also might explain it.
Feb 28, 2018 6:32 AM
I found that usually after closing the terminal or restarting my mac I found the alias undefined and the terminal informing me » -sh: htdocs: command not found «
what I should do to keeping it for every .
Mar 3, 2018 11:50 PM
1 — I put this line in the being of my .profile_bash
/».bash_profile but claims to be $0 running under $
I put similar lines in all my profiles.
here is what I see when I start a terminal session.
Yes, my .bash_profile calls config/profile.bash. The /etc/profile and /etc/bashrc are system stuff.
2 — run the alias command to see what was aliased.
3 — put an echo before and after your alias command.
4— put the alias command alone «alias» after your aliasing to see if your command worked.
Doing this will help debug your system.
Mar 4, 2018 11:18 AM
You are NOT running ‘bash’, so .bash_profile is not being invoked
the -sh says you are running sh. If you were running bash it would say -bash : but it does not.
What does the following command say:
If the output is
then you need to use .profile
Or you need to change your shell to the default bash.
It is possible that the dscl command will say /bin/bash. If that is the case, then it is possible you have your Terminal -> Preferences -> General -> Shells to open with: (X) Command (complete path): /bin/sh
Mar 4, 2018 1:20 PM
Dears Guys,
First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me
Thanks for every one
Mar 5, 2018 12:09 AM
First I would like every one for helping me, It’s working well now after I changed the alias to .profile as BobHarris saidto me
Some information to keep in the back of your mind.
By default macOS sets the default shell to bash. The fact that you are using sh means there has been some change made to your macOS setup. Whether you did this intentionally, or by accident, you should keep this in mind.
And the reason you should keep this in mind is that if you create a new account, or switch to a different Mac, you may find that in the new environment bash is now your shell, and assumptions you were making in your current environment are not working the same in the new environment.
And now that you have BOTH a .bash_profile _AND_ a .profile, you should know that if you magically switch back to bash, that when bash starts up it looks for one of the following 3 initialization files in the following order, and will use the first it finds, then ignore all the others
I mention this, because if you end up back with bash in your current account, it will start using the .bash_profile you currently have, and ignore the .profile you may have built up. This will be confusing, so keep this bit of information in the back of your mind. Unless of course you delete the .bash_profile, then bash will also find your .profile.
Источник
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
A n bash shell alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to
/.bashrc file. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
This post shows how to create and use aliases including 30 practical examples of bash shell aliases.
More about bash shell aliases
The general syntax for the alias command for the bash shell is as follows:
How to list bash aliases
Type the following alias command:
alias
Sample outputs:
By default alias command shows a list of aliases that are defined for the current user.
How to define or create a bash shell alias
To create the alias use the following syntax:
In this example, create the alias c for the commonly used clear command, which clears the screen, by typing the following command and then pressing the ENTER key:
Then, to clear the screen, instead of typing clear, you would only have to type the letter ‘c’ and press the [ENTER] key:
How to disable a bash alias temporarily
How to delete/remove a bash alias
You need to use the command called unalias to remove aliases. Its syntax is as follows:
In this example, remove the alias c which was created in an earlier example:
You also need to delete the alias from the
/.bashrc file using a text editor (see next section).
How to make bash shell aliases permanent
The alias c remains in effect only during the current login session. Once you logs out or reboot the system the alias c will be gone. To avoid this problem, add alias to your
The alias c for the current user can be made permanent by entering the following line:
Save and close the file. System-wide aliases (i.e. aliases for all users) can be put in the /etc/bashrc file. Please note that the alias command is built into a various shells including ksh, tcsh/csh, ash, bash and others.
A note about privileged access
You can add code as follows in
A note about os specific aliases
You can add code as follows in
30 bash shell aliases examples
You can define various types aliases as follows to save time and increase productivity.
#1: Control ls command output
The ls command lists directory contents and you can colorize the output:
#2: Control cd command behavior
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
#3: Control grep command output
grep command is a command-line utility for searching plain-text files for lines matching a regular expression:
#4: Start calculator with math support
#4: Generate sha1 digest
#5: Create parent directories on demand
mkdir command is used to create a directory:
#6: Colorize diff output
You can compare files line by line using diff and use a tool called colordiff to colorize diff output:
#7: Make mount command output pretty and human readable format
#8: Command short cuts to save time
#9: Create a new set of commands
#10: Set vim as default
#11: Control output of networking tool called ping
#12: Show open ports
Use netstat command to quickly list all TCP/UDP port on the server:
#13: Wakeup sleeping servers
Wake-on-LAN (WOL) is an Ethernet networking standard that allows a server to be turned on by a network message. You can quickly wakeup nas devices and server using the following aliases:
#14: Control firewall (iptables) output
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders.
#15: Debug web server / cdn problems with curl
#16: Add safety nets
#17: Update Debian Linux server
apt-get command is used for installing packages over the internet (ftp or http). You can also upgrade all packages in a single operations:
#18: Update RHEL / CentOS / Fedora Linux server
yum command is a package management tool for RHEL / CentOS / Fedora Linux and friends:
#19: Tune sudo and su
#20: Pass halt/reboot via sudo
shutdown command bring the Linux / Unix system down:
#21: Control web servers
#22: Alias into our backup stuff
#23: Desktop specific – play avi/mp3 files on demand
#24: Set default interfaces for sys admin related commands
vnstat is console-based network traffic monitor. dnstop is console tool to analyze DNS traffic. tcptrack and iftop commands displays information about TCP/UDP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.
#25: Get system memory, cpu usage, and gpu memory info quickly
#26: Control Home Router
The curl command can be used to reboot Linksys routers.
#27 Resume wget by default
The GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, and it can resume downloads too:
#28 Use different browser for testing website
#29: A note about ssh alias
Do not create ssh alias, instead use
/.ssh/config OpenSSH SSH client configuration files. It offers more option. An example:
You can now connect to peer1 using the following syntax:
$ ssh server10
#30: It’s your turn to share…
Conclusion
This post summarizes several types of uses for *nix bash aliases:
- Setting default options for a command (e.g. set eth0 as default option for ethtool command via alias ethtool=’ethtool eth0′ ).
- Correcting typos (cd.. will act as cd .. via alias cd..=’cd ..’ ).
- Reducing the amount of typing.
- Setting the default path of a command that exists in several versions on a system (e.g. GNU/grep is located at /usr/local/bin/grep and Unix grep is located at /bin/grep. To use GNU grep use alias grep=’/usr/local/bin/grep’ ).
- Adding the safety nets to Unix by making commands interactive by setting default options. (e.g. rm, mv, and other commands).
- Compatibility by creating commands for older operating systems such as MS-DOS or other Unix like operating systems (e.g. alias del=rm ).
I’ve shared my aliases that I used over the years to reduce the need for repetitive command line typing. If you know and use any other bash/ksh/csh aliases that can reduce typing, share below in the comments.
Conclusion
I hope you enjoyed my collection of bash shell aliases. See
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Nice list; found a couple new things I never thought of. To return the favor; my addon..
A nice shell is key in bash imo; I color code my next line based on previous commands return code..
And I liked your . <1,2,3,4>mapping; how I integrated it…
And two random quick short ones..
The following is my version of the “up function” I came up with this morning:
Show text file without comment (#) lines (Nice alias for /etc files which have tons of comments like /etc/squid.conf)
@linuxnetzer, nocommand is nice to dump squid, httpd and many others config files.
@mchris, I liked cp alias that can show progress.
Appreciate your comments.
Ctrl+L is also a nice quick way to clear the terminal.
Hi!
This isn’t an alias, but for clear screen is very handy the CTRL+L xDD
Источник