Make a unix alias with python script

0 votes

I want to make a python script(.py) make a UNIX alias that can be accesed after the program has ran. How can i do this?

I have tried this:


import os

os.system('alias cdd="cd ~/Desktop/"')

Sep 10, 2018 in Python by bug_seeker
• 15,300 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The scope of an alias command is the shell it is run in. When that shell exits and a new one starts, it will not have the alias defined.

The way persistent customization is provided to shells is with shell scripts that are run when the shell first starts up. For example, bash will run either or both of .bash_profile and .bashrcdepending on how it is invoked and other local configuration. Refer to the documentation for the shell you're interested in interacting with for all of the specific details of how it handles this.

If you want persistent configuration changes to a shell, you need to modify the shell's startup scripts.

os.system runs a shell and tells the shell to run the command you supplied. Then the shell exits. So running an alias command with os.system can basically accomplish nothing useful.

Here's an example of how you might adjust your bash configuration to define an alias persistently:

with open(expanduser("~/.bashrc"), "at") as bashrc:
        "# Added by myprogram on somedate\n"
        "alias cdd='cd ~/Desktop/'\n"
answered Sep 10, 2018 by Priyaj
• 56,140 points

Related Questions In Python

+2 votes
2 answers

How to make a laplacian pyramid using OpenCV python?

down voteacceptTheeThe problem is that you're iterating ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
+1 vote
3 answers
0 votes
1 answer

how to exit a python script in an if statement

This works fine for me: while True: ...READ MORE

answered Sep 19, 2018 in Python by bug_seeker
• 15,300 points
0 votes
1 answer

How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

answered Oct 31, 2018 in Python by Anirudh
• 2,050 points

edited Dec 14, 2018 by Anirudh 111 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
0 votes
1 answer

Make a unix alias with python script

The scope of an alias command is the shell ...READ MORE

answered Sep 20, 2018 in Python by Priyaj
• 56,140 points
+1 vote
1 answer

How do you make a block comment in python?

''' This is a multiline comment. I ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,140 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.