You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.9 KiB
1.9 KiB
| Title | date | Author | Draft | Categories | Tags | Truncated |
|---|---|---|---|---|---|---|
| Bash Tips & Tricks | 2018-11-11T16:01:15+01:00 | Olivier De Ram | false | [administration development] | [bash script] | true |
Usefull bash tips & tricks
Usefull CLI shortcuts:
| Shortcut | Description |
|---|---|
| Ctrl+a | Jump to the beginning of the command line. |
| Ctrl+e | Jump to the end of the command line. |
| Ctrl+u | Clear from the cursor to the beginning of the command line. |
| Ctrl+k | Clear from the cursor to the end of the command line. |
| Ctrl+Left Arrow | Jump to the beginning of the previous word on the command line. |
| Ctrl+Right Arrow | Jump to the end of the next word on the command line. |
| Ctrl+r | Search the history list of commands for a pattern. |
| Esc + . | Copy the last word of the previous command on the current command line where the cursor is |
Redirect output:
| Command | result |
|---|---|
> file |
redirect stdout to overwrite a file |
>> file |
redirect stdout to append to a file |
2> file |
redirect stderr to overwrite a file |
2> /dev/null |
discard stderr error messages by redirecting to /dev/null |
&> file (OR > file 2>&1) |
redirect stdout and stderr to overwrite the same file |
&>> (OR >> file 2>&1) |
redirect stdout and stderr to append to the same file |
Create a function:
It's straightforward, use function to create a function, give it a usefull short name and put the statemants between curly brackets.
Use $1 for the first argument, $2 for the second argument and so on...
function <name> {
<bashcommands>
}
For example the function logsearch which searches for the given string whitin all logfiles in the current directory:
function logsearch {
zgrep $1 ./*log
}
Short scripts:
- Loop every file in path:
for F in /path/to/files/*;
do
echo "Files $F";
done
- Loop file line per line:
while read LINE;
do
echo $LINE;
done < file.txt