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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-27-2009, 07:03 AM
|
#1
|
Member
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282
Rep:
|
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.
|
|
|
12-27-2009, 07:32 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
12-27-2009, 09:35 AM
|
#3
|
Member
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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.
|
|
|
|
12-27-2009, 10:15 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by clifford227
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.
|
|
|
12-27-2009, 11:17 AM
|
#5
|
Member
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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 
|
|
|
12-27-2009, 02:02 PM
|
#6
|
Member
Registered: Jun 2009
Posts: 43
Rep:
|
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
|
|
|
12-27-2009, 06:25 PM
|
#7
|
Member
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282
Original Poster
Rep:
|
Quote:
Originally Posted by dickey
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.
|
|
|
12-28-2009, 09:53 AM
|
#8
|
Member
Registered: Jun 2009
Posts: 43
Rep:
|
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).
|
|
|
12-28-2009, 08:12 PM
|
#9
|
Member
Registered: Dec 2009
Distribution: Slackware 14
Posts: 282
Original Poster
Rep:
|
thanks dickey, thats really interesting 
|
|
|
All times are GMT -5. The time now is 11:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|