LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2009, 07:03 AM   #1
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Rep: Reputation: 64
Starting Bash & Xterm in a particular directory


Hi,

I use the shell alot for various tasks, and I was wondering if it is possible to set Bash (in an Xterm) to jump to a particular directory in the file system, e.g ~/home/user/documents? instead of the default (~/) users home dir.
 
Old 12-27-2009, 07:32 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
How do you usually launch xterm? Here are a couple of suggestions: from the command line you can launch a new xterm with /home/user/documents as working directory by issuing
Code:
xterm -hold -e "cd $HOME/documents && /bin/bash" &
moreover you can put this code in a shell function and have a shortcut command at hand.

If using the KDE menu you can try to edit the properties of the xterm launcher. In KDE 3.5 I have a text box called "Work path". I can choose any valid path and every time I start a new xterm, it will open in the specified directory instead of home.
 
Old 12-27-2009, 09:35 AM   #3
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by colucix View Post
Code:
xterm -hold -e "cd $HOME/documents && /bin/bash" &
This worked


Is it possible to list the directory with an ls alias?


Code:
xterm -hold -e "cd $HOME/documents && /bin/bash & ls" &

will open Xterm and Bash at the required dir and simple list, but I cant seem to use the alias I created 'lm' which is ls -la.

Bash gives me this error:

Quote:
Bash: lm: Command not found.
 
Old 12-27-2009, 10:15 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by clifford227 View Post
Is it possible to list the directory with an ls alias?
I'm afraid not. The -e option of xterm accepts only commands with their full path or under the directories in $PATH. Aliases and functions aren't valid. Anyway you can still explicitly use aliases in the newly open terminal.
 
Old 12-27-2009, 11:17 AM   #5
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by colucix View Post
I'm afraid not. The -e option of xterm accepts only commands with their full path or under the directories in $PATH. Aliases and functions aren't valid. Anyway you can still explicitly use aliases in the newly open terminal.
Darn.

Thankyou for your help Colucix
 
Old 12-27-2009, 02:02 PM   #6
dickey
Member
 
Registered: Jun 2009
Posts: 43

Rep: Reputation: 18
Your aliases are probably being source'd only in
a login shell. The -e option doesn't act like
a login shell. But you can easily construct a
short script that does what you want. Call that
cd+ls, something like (untested...):

#!/bin/bash
# read your aliases (perhaps not the right file)
source ~/.bash_profile
# cd to the first parameter
if test $# != 0
then
cd "$1"
shift 1
fi
# run the rest of the parameters as a command
exec "$@"

So you could use something like this
(given a script named "cd+ls" in your $PATH):

xterm -hold -e cd+ls /bin ls -l
 
Old 12-27-2009, 06:25 PM   #7
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by dickey View Post
Your aliases are probably being source'd only in
a login shell. The -e option doesn't act like
a login shell. But you can easily construct a
short script that does what you want. Call that
cd+ls, something like (untested...):

#!/bin/bash
# read your aliases (perhaps not the right file)
source ~/.bash_profile
# cd to the first parameter
if test $# != 0
then
cd "$1"
shift 1
fi
# run the rest of the parameters as a command
exec "$@"

So you could use something like this
(given a script named "cd+ls" in your $PATH):

xterm -hold -e cd+ls /bin ls -l
Dickey,

My aliases are in .bashrc, and my .bash_profile sources it.

I am fairly new to scripting, although I understand some basics, but could you explain the parts of your script, I dont understand.
 
Old 12-28-2009, 09:53 AM   #8
dickey
Member
 
Registered: Jun 2009
Posts: 43

Rep: Reputation: 18
comment on script

It's all found in "man bash", which is rather long
(even longer than xterm's manpage). However

#!/bin/bash
tells the operating system where to find the shell.
From here on, it's read by bash.
# read your aliases (perhaps not the right file)
source ~/.bash_profile
tells bash to read/interpret the given file.
# cd to the first parameter
if test $# != 0
ask if there's a command-line parameter to this script
then
cd "$1"
change working directory to the given parameter (quoting in case
there are blanks).
shift 1
discard the first parameter.
fi
# run the rest of the parameters as a command
exec "$@"
transfer control to whatever command
is given in the remaining parameters.
(If there are no parameters, not much
will happen).
 
Old 12-28-2009, 08:12 PM   #9
clifford227
Member
 
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282

Original Poster
Rep: Reputation: 64
thanks dickey, thats really interesting
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xterm - howto lauch an xterm into a specific directory jobano Linux - Software 11 01-30-2023 05:45 AM
BASH Script --> read a file & cp -r to my ~/ directory manwithaplan Linux - Newbie 6 10-06-2009 12:25 AM
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 1 09-02-2009 06:28 AM
fail to start xterm: "could not exec /usr/OSE/bash: No such file or directory" freeindy Linux - General 3 11-28-2008 01:22 AM
Bash Expression: Current Directory & All Subdirectories Below gmcauley Programming 2 01-01-2007 03:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration