9

I'm very new to Linux environment and I just got myself a Pi to play with it. Recently I found this command to check my Pi's temperature which is /opt/vc/bin/vcgencmd measure_temp

Is it possible to save that command somewhere with a shortcut or an alias? So that I could just type in like checktemp and it would run the command.

Ignore me if I'm asking a nonsense question.

Lukas Knuth
  • 949
  • 2
  • 7
  • 19
Ye Myat Aung
  • 193
  • 1
  • 1
  • 4

6 Answers6

11

You can use the alias command. To make it persistent, you'll have to modify the ~/.bash_aliases file.

  1. Open the ~/.bash_aliases file with your favorite editor (nano, vim, ...)

    $ nano ~/.bash_aliases
    
  2. Add to the end of the file the following line

    alias checktemp='/opt/vc/bin/vcgencmd measure_temp'
    

    , where checktemp is the command you want to use.

  3. Exit your bash session:

    $ exit
    

    Then log in.

  4. You are ready to use your brand new command:

    $ checktemp
    temp=48.7'C
    
Morgan Courbet
  • 3,703
  • 3
  • 22
  • 38
0

To save an alias, you can use .bashrc or .bash_aliases, although on other distributions other than Raspbian .bashaliases may not be available. Here's how to make the alias last:

First, open .bashrc or .bash_aliases, like this:

nano ~/.bashrc

Go to the end of file and add your alias (also remember that there must be no space before and after the equal "=" sign, to prevent errors).

alias checktemp='sudo /opt/vc/bin/vcgencmd measure_temp'

Then save the file. If you used .bashrc, then run:

source .bashrc

Or instead you can reboot the system.

After that, the alias should work fine.

  • 1
    You wrote: ".bash_aliases may not be available in every distribution", but on Raspbian it is, so no need to mention it. You can also use .bash_aliases and I would prefer it. After modifying .bashrc it is really not needed to reboot the whole computer. Logout and login or even better just execute source .bashrc. – Ingo Jul 28 '19 at 10:54
0

Don't need to exit bash, just reload your profile.

From anywhere: $ . ~/.profile

From your home dir: $ . .profile

Jeff
  • 1
0

What they did above was good. FYI .bash_aliases is not there by default in new Raspian OS. If you are using a non-root user to run these change the above that Morgan Courbet Said to this:

alias checktemp='sudo /opt/vc/bin/vcgencmd measure_temp'

Then reload like the others said like this:

. ~/.bash_aliases

So you can add a .bash_aliases and still work. I think its better than putting into another file since you usually want many aliases to make it easier. Also, for the sudo command to work, you need to have you user in visudo like this.

visudo

anthony ALL=(ALL:ALL) NOPASSWD: ALL

I hope this helps clarify some.

Tony-Caffe
  • 101
  • 2
0

I just added this to my .bashrc

sudo nano .bashrc

Went down to the bottom of the fil (CTRL-V)

alias temp='/opt/vc/bin/vcgencmd measure_temp'

(CTRL-O) (CTRL-X)

sudo reboot

now I just type temp and it displays it.

I tried the .bash_aliases but couldn't seem to get it to work, but I am new to this so :)

-2

This article explains the command you are looking for: alias command

  • 1
    Welcome to the Rapsberry PI StackExchange. Generally speaking, link only answers are against best practices. While it's important to cite sources, a better answer would include at least a high level summary of the links content and an example. Links have a tendency to "rot". – Jacobm001 Jun 11 '15 at 15:39