Använda miljövariabler i Terminal på datorn
Kommandotolken använder miljövariabler till att lagra information som namnet på den aktuella användaren, namnet på värddatorn och de förvalda sökvägarna till kommandon. Miljövariabler ärvs av alla kommandon som körs i kommandotolkens kontext, och vissa kommandon är beroende av miljövariabler.
Du kan skapa miljövariabler och använda dem till att styra ett kommandos beteende utan att ändra själva kommandot. Till exempel kan du använda en miljövariabel till att få kommandot att skriva ut felsökningsinformation i konsolen.
Du ställer in värdet för en miljövariabel genom att använda det lämpliga kommandot för kommandotolken till att associera ett variabelnamn med ett värde. Om du till exempel vill ställa in variabeln PATH till värdet /bin:/sbin:/user/bin:/user/sbin:/system/Library/
anger du följande kommando i ett Terminal-fönster:
% PATH=/bin:/sbin:/user/bin:/user/sbin:/system/Library/ export PATH
Du kan visa alla miljövariabler genom att ange:
% env
När du öppnar en app från en kommandotolk ärver appen en stor del av kommandotolkens miljö, inklusive exporterade miljövariabler. Den här sortens arv kan vara ett praktiskt sätt att konfigurera appen dynamiskt. Till exempel kan din app kontrollera närvaron (eller värdet) för en miljövariabel och ändra sitt beteende därefter.
Olika kommandotolkar stöder olika semantik för exporten av miljövariabler. Se man-sidan för den kommandotolk du föredrar.
Även om en kommandotolks underordnade processer ärver den kommandotolkens miljö är kommandotolkar ändå separata körningskontexter som inte delar miljöinformation med varandra. Variabler som du ställer in i ett Terminal-fönster ställs inte in i andra Terminal-fönster.
När du har stängt ett Terminal-fönster är variablerna som du har ställt in i det fönstret inte längre tillgängliga. Om du vill att värdet för en variabel ska bevaras mellan sessioner och i alla Terminal-fönster måste du ställa in det i ett startskript för kommandotolken. Läs mer i avsnittet ”Invocation” på man-sidan för zsh om hur du ändrar startskriptet för kommandotolken zsh så att variabler och andra inställningar sparas mellan flera sessioner.