-
-
Notifications
You must be signed in to change notification settings - Fork 525
004-bash-variables [es] #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bobbyiliev
merged 2 commits into
bobbyiliev:main
from
marcelozarate:es-004-bash-variables
Oct 4, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
ebook/es/content/004-bash-variables.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Variables en Bash | ||
|
|
||
| Como en cualquier otro lenguaje de programación, también puede usar variables en Bash scripting. Sin embargo, no hay tipos de datos, y una variable en Bash puede contener tanto números como caracteres. | ||
|
|
||
| Para asignar un valor a una variable, todo lo que necesita hacer es usar el signo `=`: | ||
|
|
||
| ```bash | ||
| nombre="DevDojo" | ||
| ``` | ||
|
|
||
| >{notice} como nota importante, no puede haber espacios antes y después del signo `=`. | ||
|
|
||
| Después de eso, para acceder a la variable, debe utilizar el `$` y la referencia como se muestra debajo: | ||
|
|
||
| ```bash | ||
| echo $nombre | ||
| ``` | ||
|
|
||
| Envolver el nombre de la variable entre llaves no es obligatorio, pero se considera una buena práctica, y le aconsejo que las use siempre que pueda: | ||
|
|
||
| ```bash | ||
| echo ${nombre} | ||
| ``` | ||
|
|
||
| El código anterior daría como resultado `DevDojo` ya que este es el valor de nuestra variable `nombre`. | ||
|
|
||
| A continuación, vamos a actualizar nuestro script `devdojo.sh` e incluir una variable en él. | ||
|
|
||
| De nuevo, puede abrir el archivo `devdojo.sh` con su editor de texto favorito, yo estoy usando nano aquí para abrir el archivo: | ||
|
|
||
| ```bash | ||
| nano devdojo.sh | ||
| ``` | ||
|
|
||
| Añadiendo nuestra variable `nombre` aquí en el archivo, con un mensaje de bienvenida. Nuestro archivo ahora se ve así: | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
|
|
||
| nombre="DevDojo" | ||
|
|
||
| echo "Hola $nombre" | ||
| ``` | ||
|
|
||
| Guárdelo y ejecute el archivo con el siguiente comando: | ||
|
|
||
| ```bash | ||
| ./devdojo.sh | ||
| ``` | ||
|
|
||
| Verá la siguiente salida en su pantalla: | ||
|
|
||
| ```bash | ||
| Hola DevDojo | ||
| ``` | ||
|
|
||
| Aquí está un resumen del script escrito en el archivo: | ||
|
|
||
| * `#!/bin/bash` - Al principio, especificamos nuestro shebang. | ||
| * `nombre=DevDojo` - Luego, definimos una variable llamada `nombre` y le asignamos un valor. | ||
| * `echo "Hola $nombre"` - Finalmente, mostramos el contenido de la variable en la pantalla como un mensaje de bienvenida usando `echo`. | ||
|
|
||
| También puede añadir múltiples variables en el archivo como se muestra debajo: | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
|
|
||
| nombre="DevDojo" | ||
| saludo="Hola" | ||
|
|
||
| echo "$saludo $nombre" | ||
| ``` | ||
|
|
||
| Guarde el archivo y ejecútelo de nuevo: | ||
|
|
||
| ```bash | ||
| ./devdojo.sh | ||
| ``` | ||
|
|
||
| Verá la siguiente salida en su pantalla: | ||
|
|
||
| ```bash | ||
| Hola DevDojo | ||
| ``` | ||
|
|
||
| Tenga en cuenta que no necesariamente tiene que añadir punto y coma `;` al final de cada línea. Funciona de ambas maneras, un poco como otros lenguajes de programación como JavaScript. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.