Creating a command on a BASH shell

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a command on a BASH shell
# 1  
Old 08-06-2009
Creating a command on a BASH shell

Hi all. Suppose I have the following function in an executable file named "HOLA":
function hola { echo "Hola ${@}."; }

In addition, suppose that I want to execute the file so I can input my name next to ./HOLA. I mean,
$ ./HOLA hugo esquivel

According to the function hola, the output would be:
Hola hugo esquivel.

My question is: how can I achieve this behaviour? Is possible?

Any help will be greatly appreciated!

PS: What I want is to create the command hola on a BASH shell (but through the executable).
# 2  
Old 08-06-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums


$> cat mach.ksh

function hola ()
        echo "Hola $*"

hola $*

exit 0
$> ./mach.ksh jupp selters
Hola jupp selters

You are missing the () behind the function name. Also you have to call a function when you want to use it. Also you have to give the function all the parameters you give to the shell script too. I changed $@ to $* since $@ is some thing like
"jupp" "selters"
# while $* is
"jupp selters"

Rest is formatting. It is also usually good to have a shebang in the 1st line (that #!...) to tell which shell/interpreter you want to use for this script, and also a "exit 0" can't harm if you are going to check exit stati.
# 3  
Old 08-06-2009
Use CODE-tags. Not colors, bold, flash, ...

# or ksh

# call function like any other command type (builtin, external, alias, function, ...)
# = function get arguments from function call line
function hola
    echo "Hola $@."


# save script arguments to the variable

# run command - in this case command is function
hola $CmdLineArgs

Function, how to define ?
Old bsh style, which is also Posix-standard =
the most generic.

or alternative (ksh, bash, ...)
function hola

Or mixed - only in bash = not so generic
function hole()

Last edited by kshji; 08-06-2009 at 04:14 AM..
# 4  
Old 08-06-2009
Thanks a lot!

Next time I will take into account the time to format my posts correctly.

You said:
You are missing the () behind the function name.
Actually if you want, you might miss them, according to the "Bash Reference Manual" (February 2009). Smilie

---------- Post updated at 06:39 AM ---------- Previous update was at 06:33 AM ----------

Thank you!
# 5  
Old 08-06-2009
From man bash on my Debian box Smilie
       [ function ] name () compound-command [redirection]
              This defines a function named name.  The reserved word function is optional.  If the function reserved word is supplied, the parentheses
              are optional.  The body of the function is the compound command compound-command (see Compound Commands above).  That command is usually
              a  list of commands between { and }, but may be any command listed under Compound Commands above.  compound-command is executed whenever
              name is specified as the name of a simple command.  Any redirections (see REDIRECTION below) specified when a function  is  defined  are
              performed  when  the  function is executed.  The exit status of a function definition is zero unless a syntax error occurs or a readonly
              function with the same name already exists.  When executed, the exit status of a function is the exit status of the  last  command  exe-
              cuted in the body.  (See FUNCTIONS below.)

Tbh, I usually write them even without "function" in front of them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Which of the following command displays your login shell in bash shell?

Options:: A)$shell B)echo $ bash C)echo $ O D)$ O (1 Reply)
Discussion started by: raghugowda
1 Replies

2. Programming

Creating a bash based restricted shell

Hello. I need to write a command line interface that can be invoked either directly from the shell (command sub-command arguments), or as a shell that can process sub-commands. i want to use bash auto completion for both scenarios. example: lets say my CLI module is called 'mycli' and there... (5 Replies)
Discussion started by: noamr
5 Replies

3. Shell Programming and Scripting

creating own command interpreter like bash...??

Hello senior members, I am a fairly newbie here. I just want to ask one question that how can once create one's own command interpreter/ shell like bash in unix/linux. I need to execute basic commands like pipes and i/o. Any help in this matter ?? (3 Replies)
Discussion started by: duma188
3 Replies

4. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

5. UNIX for Dummies Questions & Answers

unix command - bash shell

Hey all, I need to know how many lines of C code are in a source bundle. I have extracted the bundle and hence there is a big directory tree with C files everywhere. So far I have something like: find . -name "*.c" | grep \ ./*/*/*/*/*/* obviously wrong, if someone could help me out... (3 Replies)
Discussion started by: zigga15
3 Replies

6. UNIX for Dummies Questions & Answers

more command does not work in bash shell

is there a different command to display contents of a file on the output in bash shell? i tried more and it does not work. (7 Replies)
Discussion started by: npatwardhan
7 Replies

7. Shell Programming and Scripting

Bash shell: Creating Preferences

In OS X I'm currently writing a bash script that requires writing to preference file. I may eventually want to share it with users on other Unix-like OSs and would like to accommodate for that possibility ahead of time. Most OS X applications save preferences in xml-format plist files. These... (4 Replies)
Discussion started by: airsmurf
4 Replies

8. Shell Programming and Scripting

Creating a command history feature in a simple UNIX shell using C

I'm trying to write a history feature to a very simple UNIX shell that will list the last 10 commands used when control-c is pressed. A user can then run a previous command by typing r x, where x is the first letter of the command. I'm having quite a bit of trouble figuring out what I need to do, I... (2 Replies)
Discussion started by: -=Cn=-
2 Replies

9. Shell Programming and Scripting

Using variable with cp command in bash shell

Hi, I am trying to write script to copy some files(/ppscdr/cdrp2p/temp/) from one directory to another directory using shell script. see the script below, #!/bin/sh -f dir_name=20061105 mkdir ${dir_name} cd /ppscdr/cdrp2p/temp pwd cp p2p${dir_name}*.*... (4 Replies)
Discussion started by: maheshsri
4 Replies

10. Shell Programming and Scripting

creating file of 1MB using shell command?

Hi everybody in the forum, I want to create an empty file of say some 1MB ,i mean at the command line itself.How is this possible??????EEK! (4 Replies)
Discussion started by: vijaya2006
4 Replies
Login or Register to Ask a Question