how to write a function in bash_profile

bash_profile function arguments
bash function
bash function with arguments
bashrc function arguments
useful bash functions
bashrc tutorial
making bash functions

how can we write a simple regular function which i can put in my bashprofile which can be used to secure console to any host i want.

but my secure console has to go through a jump host. that is the issue.

function func_name () {
 ssh jumphostname; 
 sc $hostname # from jump host secure console to another host given as input from terminal

this function only making to login in to jump host but not to secureconsole in to another host from there.

-bash-4.1$func_name should give me console to via jumphost

is function for this not possible? do i have to write a script?

You should not use commands in a test [ ] unless you simulate a variable with $( ) arround the commands. Still not sure SSH will return something to the test. SSH needs the TTY you like connect to, and not the TTY you in at. This will causes problems!

An example without SSH ...

suleiman@antec:~$ if [ "$(cat ~/test.txt)" ]; then echo "Hello World"; else echo "failed"; fi
Hello World
suleiman@antec:~$ if [ "$(cat /this/file/dont/exsist 2>/dev/null)" ]; then echo "Hello World"; else echo "failed"; fi


-bash: sc: command not found

This means you have NOT installed the spreadsheet on the host.

This function only making to login in to jump host but not to secureconsole in to another host from there.

What you trying to do ?

Do you know what SSH does ? It opens remote TTYs, or with other words: it opens a remote secure console.

You cant run a script and put somewhere a ssh login in it, and then think all code after that will be in the new console, neither will that happen.

You can run ssh on a console, so you get your own TTY and put some commands in it. Or you use ssh in combination with some commands in a script, like

ssh user@host echo "Hello World!"

You can also pass some variables or text though ssh via

echo "Hello World!" | ssh user@host cat

There isnt much more you can do with it and you shouldn't!

How to add a function to .bash_profile/.profile/bashrc in shell?, From man bash : When bash is invoked as an interactive login shell, or as a non-​interactive shell with the --login option, it first reads and  The commands below are laid out in a more-or-less narrative style, so if you're just getting started with bash, you can work your way through from the beginning to the end. Things generally get less common and more difficult toward the end. Table of Contents. First Commands, Navigating the Filesystem. Viewing and Editing Files.

I would write this

con.sole() {
    if ! ssh -T jumphostname true; then
        printf 'Jump host "%s" not available.\n' jumphostname >&2
        return 1
    sc "$@"

The square brace isn't part of the if statement syntax. It is a separate command, the same as test.

Configuring bash with aliases and functions – Scripting OS X, This post will look at some useful aliases and functions. Here is a sample .​bash_profile with many of the examples given here and in the  ~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file It looks like you are starting a new shell and that the entries you've put in ~/.bash_profile are not used at all. Try to set the entries in the ~/.bash_rc file instead.

Below link would help you to go ahead

ssh username@host_address "command to execute"

For example output:

arul@OA2:~/work/images$ ssh arul@localhost echo "hai"
arul@localhost's password: 

ssh arul@localhost command will login and "echo hai" command printed in currently logged in prompt"


An Introduction to Useful Bash Aliases and Functions, repeatedly. Luckily, bash allows you to create aliases and and bash functions to help reduce typing. Popular choices are ~/.bashrc and ~/.bash_profile . You can also cut out the cd part entirely by making an alias for .. : Here is a base /etc/profile. This file starts by setting up some helper functions and some basic parameters. It specifies some bash history parameters and, for security purposes, disables keeping a permanent history file for the root user. It also sets a default user prompt.

Here's how I do it.

  1. Create a functions folder at home
  2. Write my function as a shell script
  3. Reference the file as an alias in my bash_profile
  4. Reset the source


mkdir ~/.functions
echo '#!/bin/bash
      echo $1' > ~/.functions/ekho
echo 'alias ekho="sh ~/.functions/ekho"' >> ~/.bash_profile
source ~/.bash_profile

Now you can call your method from any location for ever ever.

ekho "Wow"

a function in .bash_profile, How should I write this function? about kill: my python process is multithreaded.I'​ve tried kill pid , kill -TERM pid ,or kill  Save your file, and then make sure that it’s set executable. You can do this using the chmod utility, which changes a file’s mode. To set it so that a script is executable by you and not the rest of the users on a system, use “chmod 700 scriptname” — this will let you read, write, and execute (run) the script — but only your user.

Its because you dont leave a whitespace between the if and the [... The the correct sintax you want is...

function con.sole
    if [ ssh jumphostname ]; then
        sc $1;
        echo "host not available"

Greetings from Mexico! 🇲🇽

Sample .bashrc and .bash_profile Files, Study the file carefully, and feel free to reuse code snippets and functions from it in your own .bashrc file or even in your scripts. Example M-1. Sample .bashrc  This article intends to help you to start programming basic-intermediate shell scripts. It does not intend to be an advanced document (see the title). I am NOT an expert nor guru shell programmer. I decided to write this because I'll learn a lot and it might be useful to other people.

How to Create Custom Terminal/Shell Commands, For example, navigating to certain directories in terminal, quickly running quicker and easier in the future because open ~/.bash_profile isn't pretty. Open your bash profile and type in the following custom function by your  That makes sense. I had been working on CentOS and Darwin for a few days and both seem to run GUI logins through bash shells (allowing function exports from /etc/profile and .bash_profile that become available to all subsequent bash shells). Lesson learnt :) – Ian Mackinnon Feb 16 '11 at 23:02

Using Bash Functions To Start A Local Environment Quicker, Let's write some bash functions. Open your terminal and type the following to get into your .bash_profile so we can write a function or two: Bash scripting relies on an understanding of the Linux Command Line. If you are not confident on the Linux Command Line I suggest you start by going through our Linux Tutorial. The best way to learn Bash Scripting is as a series of small, easy to manage steps. This tutorial is organised as such, with each section building upon the knowledge and

Functions | The Bourne Again Shell, You can declare a shell function in the ~/.bash_profile startup file, in the script The next example shows how to create a simple function that 

  • No you need to be much more specific, and I suggest you re-word your question to be more appropriate, as this isn't so much about how to write a bash function, but more about ssh remote control from a local station.
  • When I try to put together what you're trying to do, I end up with wondering why you can't just ssh user@host and work from there ? Like I said, if you are more specific, and perhaps give a usage case scenario, then people might be able to help. For versatile remote control and exception handling, I often use expect wrappers
  • yes. i made the question more specific.
  • we create alias in bash_profile. it goes through. But after logging in i want to run sc command on that host from that jump host.
  • ssh user@host sc is what you looking for ?.. I updated my post.
  • above script gets me till jumphost. it cant sc after that con.sole Warning: Permanently added ' (RSA) to the list of known hosts. -bash: sc: command not found
  • Welcome to StackOverflow. Please note that link-only answers can usually be improved by actually explaining your intended answer yourself, including anything directly relevant from the link, and linking only for further information. See e.g.…
  • Thanks for your valuable inputs @kojiro
  • after leaving space also it had -bash: [: ssh: unary operator expected so i added single quotes. it works now but real problem still exists.
  • What are trying to do? Do you want to connect a server and then connect to another?... local->server1->server2?
  • I think you need to learn what [ means in shell; it's not a grouping operator.