Guía básica de Bash

Guía básica de Bash

Comandos

Listado de comandos

  • Last command and all arguments: !!
  • Third-to-last command and all arguments: !-3
  • First argument of last command: !^
  • Second argument of last command: !:2
  • Last argument of last command: !$
  • All arguments of the last command: !*
  • Expands to the 42nd command in history: !42
  • Last command beginning with ‘foo’: !foo
  • Last command containing ‘foo’: !?foo
  • Last command with first instance of ‘foo’ replaced with 'bar': ^foo^bar
  • Last command with all instances of ‘foo’ replaced with ‘bar: !:gs/foo/bar
  • Don’t execute and print command: <command>:p

Renombrar archivos

rename "s/JPG/jpg/" *.JPG

Editar la línea con el editor predefinido del sistema

<C-x C-e>
# Realizas la edición y guardas
# Se ejecuta

Reemplazar una cadena de texto del anterior comando

# Ejecutamos echo:
echo 123abc
# Devuelve 123abc

^123^456
# Devuelve 456123

Utilizar un comando del historial de bash

# Ejecutamos history:
history

# Nos devuelve:
# ...
# 480  cd x
# 481  cd ..
# 482  ll
# ...

!482
# Ejecuta el comando número 482 del historial
!482:p
# Muestra y define como último comando el comando número 482 del historial

Variables

Substitución de variables

#=== Variable completa:
echo ${HOME}
# /home/usuario

#=== Deletes shortest match of $substring from front of $string:
# ${string#substring}
echo ${HOME#*/}
# home/usuario

#=== Deletes longest match of $substring from front of $string:
# ${string##substring}
echo ${HOME##*/}
# usuario

#=== Deletes shortest match of $substring from back of $string.
# ${string%substring}
echo ${HOME%/o*}
# /home/usuar

#=== Deletes longest match of $substring from back of $string.
# ${string%%substring}
echo ${HOME%%/o*}
# /h

#=== Todo a mayúsculas:
echo ${HOME^^}
# /HOME/USUARIO

#=== La primera letra en mayúsculas:
echo ${HOME^}

#=== La primera letra en minúsculas:
echo ${HOME,}

#=== Todo a minúsculas:
echo ${HOME,,}
# /home/usuario

Manipulación de variables y funciones

# Tenemos un script tal que:
HOLA='variable hola'
echo ${HOLA}
echo ${ADIOS}

Si lo ejecutamos sin set nos devuelve:

variable hola
# Devuelve un string vacío

Si lo ejecutamos con set -e

variable hola
# Devuelve un string vacío

Si lo ejecutamos con set -x nos devuelve:

+ HOLA='variable hola'
+ echo variable hola
variable hola
+ echo
# Devuelve un string vacío

Si lo ejecutamos con set -u nos devuelve:

variable hola
ADIOS: variable sin asignar

Si lo ejecutamos con set -xu nos devuelve:

+ HOLA='variable hola'
+ echo variable hola
variable hola
ADIOS: variable sin asignar

Rename

rename 'y/A-Z/a-z/' TiTulo.md
# titulo.md
Actualizado el