Last active
November 27, 2025 14:18
-
-
Save 0bvim/56e00e7733d52d583954f75088419d51 to your computer and use it in GitHub Desktop.
Function to add alias and function to add function to bash
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
| # Se você, assim como eu gosta de adicionar alias e funçoes para coisas repetitivas | |
| # essa pode ser uma boa opção | |
| # | |
| # Alias eu uso para comandos mais simples, que eu não preciso mudar argumento. | |
| # ex: make run-public-api -> eu uso como alias. | |
| # | |
| # Função eu uso em duas ocasiões, uma é para quando em um 'alias' eu teria que usar muitos | |
| # "escapes" devido a aspas ou '/'. | |
| # E a outra é quando eu preciso passar uma variável no argumento, independente do comando, | |
| # eu uso exemplo com makefile, porque é onde mais uso atualmente, mas pode ser usado em outras ocasiões. | |
| # ex: make run-<COMPANY_NAME>-worker | |
| # então eu faço uma função: | |
| # mr() | |
| # { | |
| # make run-$1-worker | |
| # } | |
| # E uso ela da seguinte maneira: | |
| # mr Github | |
| # ou | |
| # mr Cumbuca | |
| # o comando é praticamente o mesmo, só que muda um argumento e fica fácil fazer isso com função. | |
| # E tudo que e repetitivo eu faço um alias ou função para facilitar meu trabalho e tornar as coisas menos morosas. | |
| add_command_to_zsh() | |
| { | |
| if [ $# -ne 2 ]; then | |
| echo "Usage: add_command_to_zsh <command_alias> <command>" | |
| echo "Example: add_command_to_zsh 'll' 'ls -la'" | |
| return 1 | |
| fi | |
| local command_alias="$1" | |
| local command="$2" | |
| local zsh_file="$HOME/.zshrc" | |
| # Add the new command to the file | |
| echo >> "$zsh_file" | |
| echo "alias $command_alias='$command'" >> "$zsh_file" | |
| echo "✅ Added command '$command_alias' -> '$command' to zsh" | |
| echo "🔄 Restart zsh or run 'source ~/.zshrc' to apply changes" | |
| } | |
| add_function_to_zsh() | |
| { | |
| if [ $# -ne 2 ]; then | |
| echo "Usage: add_function_to_zsh <function_name> <function_body_command>" | |
| echo "Example: add_function_to_zsh 'mr' 'make run-\$1-worker'" | |
| echo "Note: Use escaped dollar signs (\\\$1, \\\$2) in the command body so they are saved literally in the file." | |
| return 1 | |
| fi | |
| local function_name="$1" | |
| local function_body="$2" | |
| local zsh_file="$HOME/.zshrc" | |
| # Define the full function syntax | |
| local full_function_definition | |
| full_function_definition="${function_name}() { ${function_body} }" | |
| # Add the new function to the file | |
| echo >> "$zsh_file" | |
| echo "# Added via add_function_to_zsh helper function" >> "$zsh_file" | |
| echo "$full_function_definition" >> "$zsh_file" | |
| echo "✅ Added function '$function_name' to $zsh_file" | |
| echo " Definition: $full_function_definition" | |
| echo "🔄 Restart zsh or run 'source ~/.zshrc' to apply changes" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment