I've attempted to write a small script that will by default open emacs withemacs -nw fooby simply typingemacs fooand when I want a window typingemacs foo &The script  I attempted to write looks like this#!/bin/bashif [ -z $2 ]; then    emacs -nw $1else    emacs $1 &fiThe problem I'm running into is that when I try to run the script nothing happens, as if I just hit enter.Would a more experienced person please advice how to make this script properly?
Script for opening emacs in two different ways
bash
The problem with the script is that if you call it either way (with and without &, it sees only one parameter. bash has already used the & to put the process in the background. If you would try the fg after starting with emacs foo & you should see emacs with no window, as with the first option. The second option never gets executed, and it should not have an &. What is needed is that the script tests if it runs in the background and decides on that. In a C program or in Python I would test if the program group and the terminal foreground process group are the same, but I  do not know how to do that in bash shell (Chris Down answer indicates it can be done).Your invocation strongly implies you want to call this script emacs in that case make sure to put the full path to the 'real' emacs in there, to prevent recursion.If you can live with a Python script to do the job:import osimport sysimport subprocessSTDOUT_FILENO=1emacs_cmd = ['/usr/bin/emacs']if os.getpgrp() == os.tcgetpgrp(STDOUT_FILENO):    emacs_cmd.append('-nw')emacs_cmd.extend(sys.argv[1:])subprocess.call(emacs_cmd)It allows you to specify multiple filenames (and additional emacs options) on the commandline.