# alias

## alias

The `alias` command in Unix-like operating systems is used to create shortcuts or aliases for other commands or sets of commands. This can save time and reduce the potential for errors when frequently using long or complex command sequences.

#### Creating Aliases

**Syntax:**

```bash
alias name='command'
```

* **name:** The name of the alias.
* **command:** The command or sequence of commands the alias represents.

#### Basic Examples

1. **Simple Aliases:**

   ```bash
   alias ll='ls -la'
   alias gs='git status'
   ```

   * `ll` will run `ls -la`, showing a detailed list of files in the directory.
   * `gs` will run `git status`, showing the status of the current Git repository.
2. **Alias with Options:**

   ```bash
   alias grep='grep --color=auto'
   ```

   * `grep` will now always run with the `--color=auto` option, which highlights matching text.
3. **Alias for Complex Commands:**

   ```bash
   alias search_logs='grep -i error /var/log/syslog'
   ```

   * `search_logs` will search for the term "error" in the `/var/log/syslog` file, case-insensitively.

#### Viewing Aliases

To list all currently defined aliases:

```bash
alias
```

#### Removing Aliases

To remove an alias, use the `unalias` command:

```bash
unalias name
```

For example:

```bash
unalias ll
```

#### Permanent Aliases

Aliases created in the terminal are temporary and will be lost when the terminal session ends. To make aliases permanent, you need to add them to your shell's configuration file.

* **For Bash:**

  Add aliases to `~/.bashrc` or `~/.bash_profile`:

  ```bash
  echo "alias ll='ls -la'" >> ~/.bashrc
  source ~/.bashrc
  ```
* **For Zsh:**

  Add aliases to `~/.zshrc`:

  ```bash
  echo "alias ll='ls -la'" >> ~/.zshrc
  source ~/.zshrc
  ```

#### Using Aliases in Scripts

Aliases are generally not expanded in non-interactive shells (such as within scripts). If you need to use an alias in a script, you can force alias expansion by including the following at the top of the script:

```bash
shopt -s expand_aliases
```

#### Example of a Script with Aliases

```bash
#!/bin/bash

# Enable alias expansion
shopt -s expand_aliases

# Define aliases
alias ll='ls -la'
alias gs='git status'

# Use aliases
ll
gs
```

#### Advanced Examples

1. **Alias with Parameters:**

   Aliases do not directly support parameters. However, you can use functions to achieve this functionality.

   ```bash
   mygrep() {
     grep --color=auto "$@"
   }
   alias grep=mygrep
   ```
2. **Chained Commands:**

   You can chain multiple commands in an alias using `&&`, `||`, or `;`.

   ```bash
   alias update='sudo apt-get update && sudo apt-get upgrade'
   ```
3. **Overriding Default Commands:**

   Be cautious when overriding default commands, as this can lead to unexpected behavior.

   ```bash
   alias rm='rm -i'
   ```

   * This alias makes the `rm` command interactive, prompting for confirmation before deleting files.

#### Conclusion

The `alias` command is a powerful tool for simplifying and customizing your command-line experience. By creating aliases, you can streamline frequently used commands and reduce the risk of errors. Remember to make aliases permanent by adding them to your shell's configuration file if you want to keep them across sessions.

## help

```
alias: alias [-p] [name[=value] ... ]
    Define or display aliases.
    
    Without arguments, `alias' prints the list of aliases in the reusable
    form `alias NAME=VALUE' on standard output.
    
    Otherwise, an alias is defined for each NAME whose VALUE is given.
    A trailing space in VALUE causes the next word to be checked for
    alias substitution when the alias is expanded.
    
    Options:
      -p        print all defined aliases in a reusable format
    
    Exit Status:
    alias returns true unless a NAME is supplied for which no alias has been
    defined.
```
