# Cut command in Bash

Eg of a Bash script that displays:

* The content of the current directory
    
* In a list format
    
* Where only the part of the name after the first dash is displayed
    

```bash
list_dir=$(ls)
for i in $list_dir; do
        echo "$i" | cut -d '-' -f2
done
```

The script uses the `ls` command to list the contents of the current directory and assigns the result to the `list_dir` variable.

Here's a breakdown of the script:

1. `list_dir=$(ls)`: This line uses the command substitution syntax `$(...)` to execute the `ls` command and store the result in the `list_dir` variable. The `ls` command lists the contents of the current directory.
    
2. `for i in $list_dir; do`: This line starts the `for` loop. It iterates over each item in the `list_dir` variable. The loop variable `i` takes each value in each iteration.
    
3. `echo "$i" | cut -d '-' -f2`: This line uses the `echo` command to print the current value of the loop variable `i`. The `|` symbol is a pipe, which redirects the output of the `echo` command to the input of the `cut` command.
    

The `cut` command is used to extract specific parts of a line or string based on a delimiter. Let's break down the command `cut -d '-' -f2`:

* `-d '-'`: This specifies the delimiter used to separate the fields in the input. In this case, the delimiter is set to `-`, meaning that the input will be split into fields whenever a `-` character is encountered.
    
* `-f2`: This specifies the field(s) to be extracted from the input. In this case, it is set to `2`, indicating that the second field after splitting the input on the delimiter will be extracted.
    

For example, let's consider the input string `"example-text"`. Using the `cut` command with `-d '-' -f2` on this input will result in `"text"` being extracted because:

* The delimiter `-` splits the input into two fields: `"example"` and `"text"`.
    
* The `-f2` option specifies that the second field, which is `"text"`, should be extracted.
    

In the context of the script, the `cut` command is used to extract the part of the name after the first dash (`-`) in each item of the directory listing. It splits the input string at the first `-` character and returns the portion after it.
